2012-11-15 2 views
1

공개 스키마에서 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> 

답변

1

내가 솔루션은 매우 가까운 것을 말할 것입니다. 너는 말했다 :

나는 DB를 전혀 바꿀 수 없다. 최대 절전 모드 4를 사용하고 있습니다. 어떻게해야합니까?

그래서 코드와 매핑을 변경할 수 있습니다. 내가 맞다 그리고 경우에 당신은 코드와 매핑을 변경할 수 있습니다 설정을 당신이 시도했습니다 inverse :

<bag name="locations" inverse="true"> 

가 그것을 해결해야한다.이 경우에만 LocationEvent에 명시 적으로 할당해야합니다 (소유 컬렉션에 넣을뿐만 아니라).

Location 매핑이 있어야 할 속성 Event

public class Location{ 
    private String locationId; 
    private Event event; 
    private double lat; 
    private double lon; 
    //getters and setters 
} 

이 있어야합니다

<class name="Location" table="location"> 
    <id name="locationId" column="location_id"/> 
    <property name="lat" column="lat"/> 
    <property name="lon" column="lon"/> 
    <many-to-one name="Event" column="event_id" /> 
</class> 

그래서 코드에서 당신이 할 경우 :

event.locations.add(location); 
location.event = event; 

그런 다음이 함께 작동합니다을 삽입물 만. 반대로 말하면, 그 아이가 모든 것을 혼자 할 것이기 때문에 부모에 대해 알아야합니다.

+0

유일한 방법은 "자녀"(위치)에 부모 (이벤트)에 대해 알리는 것입니까? 그것은 훌륭한 해결책처럼 느껴지지 않지만, 그것이 효과가 있다면 ... – dontocsata

+0

나는 내 개인적인 경험에서 분명히 정확한 해결책이라고 말할 것입니다. 'ORM' 세계에서 이것은 가장 일반적인 시나리오입니다 (종종 컬렉션이 소유자를 알지 못하는 다른 표준 JAVA 코드). 하지만 Hibernate를 사용하면 이것이 정상적이고 평범한 것을 알 수 있습니다.) –

관련 문제