공개 스키마에서 2 테이블 이벤트 & 위치가 있습니다. 공용 테이블에서 상속받는 '다른'스키마에는 두 개의 테이블이 있습니다. 이 설정을 모방 한 4 개의 클래스가 있습니다 (Event, Location, OtherEvent, & OtherLocation). 나는 Hibernate를 사용하여 OtherEvent를 지속하려고 시도하고있다. & Other Location.다 대일 하위 클래스 외래 키
모든 이벤트의 위치는 1 개 이상입니다. 위치는 이벤트에 의미가 없습니다.
OtherEvent를 추가하고이를 저장하려고 시도하면 hibernate가 OtherEvent를 other.event에 삽입합니다. & hibernate는 otherLocation을 other.location (event_id 포함)에 삽입합니다. 다음 최대 절전 모드로 시도합니다 : UPDATE public.location set event_id =? 어디 위치 =? public.location에 대한 권한이 없으므로 실패합니다. 이 업데이트를 수행하지 못하게하려면 어떻게합니까? 또는 other.location 테이블에서 업데이트를 수행하도록 변경하십시오.
나는 public.hbm.xml을 수정하고 변경 시도 :
<bag name="locations">
<bag name="locations" inverse="true">
에이 업데이트 문을 수행하지 않지만, other.location에 삽입은 포함되지 않습니다 event_id 열 (null이 아닌 경우 ConstraintViolation이 발생 함) 위치에 Event 객체에 대한 참조가 없기 때문입니다.
DB를 전혀 변경할 수 없습니다. 최대 절전 모드 4를 사용하고 있습니다. 어떻게해야합니까?
여기에 관련 CONFIGS의 모든입니다 공공 클래스 :
public class Event{
private String eventId;
private List<Location> locations;
//getters & setters
}
public class Location{
private String locationId;
private double lat;
private double lon;
//getters and setters
}
공공 테이블 :
create table public.event{
event_id varchar PRIMARY KEY,
event_name varchar,
)
create table public.location(
location_id varchar PRIMARY KEY,
event_id varchar NOT NULL, --foreign key to public.event.event_id
lat double,
lon double
)
public.hbm.xml :
<hibernate-mapping schema="public">
<class name="Event" table="event">
<id name="eventId" column="event_id"/>
<bag name="locations">
<key column="event_id" not-null="true"/>
<one-to-many class="Location">
</bag>
</class>
<class name="Location" table="location">
<id name="locationId" column="location_id"/>
<property name="lat" column="lat"/>
<property name="lon" column="lon"/>
</class>
</hibernate-mapping>
다른 테이블 :
0 12,다른 클래스 :
public class OtherEvent extends Event{
private String otherInformation;
//getter & setter
}
public class OtherLocation extends Location{
private String otherLocInformation;
//getter & setter
}
other.hbm.xml :
<hibernate-mapping schema="other">
<union-subclass name="OtherEvent" extends="Event" table="event">
<property name="otherInformation" column="other_information"/>
</union-subclass>
<union-subclass name="OtherLocation" extends="Location" table="location">
<property name="otherLocInformation" column="other_loc_information"/>
</union-subclass>
</hibernate-mapping>
유일한 방법은 "자녀"(위치)에 부모 (이벤트)에 대해 알리는 것입니까? 그것은 훌륭한 해결책처럼 느껴지지 않지만, 그것이 효과가 있다면 ... – dontocsata
나는 내 개인적인 경험에서 분명히 정확한 해결책이라고 말할 것입니다. 'ORM' 세계에서 이것은 가장 일반적인 시나리오입니다 (종종 컬렉션이 소유자를 알지 못하는 다른 표준 JAVA 코드). 하지만 Hibernate를 사용하면 이것이 정상적이고 평범한 것을 알 수 있습니다.) –