Java 多对一子类外键

Java 多对一子类外键,java,hibernate,hibernate-mapping,Java,Hibernate,Hibernate Mapping,我在公共模式中有两个表event和location。我在'other'模式中有两个表,它们继承自公共表。有4个类模拟此设置(事件、位置、其他事件和其他位置)。我正在尝试使用Hibernate来持久化OtherEvent&otherLocation 每个事件有1+个位置。一个地点对于一个事件来说毫无意义 如果我创建了一个OtherEvent并向其中添加了一个OtherLocation并试图保存它,hibernate会将OtherEvent插入other.event&hibernate会将Other

我在公共模式中有两个表event和location。我在'other'模式中有两个表,它们继承自公共表。有4个类模拟此设置(事件、位置、其他事件和其他位置)。我正在尝试使用Hibernate来持久化OtherEvent&otherLocation

每个事件有1+个位置。一个地点对于一个事件来说毫无意义

如果我创建了一个OtherEvent并向其中添加了一个OtherLocation并试图保存它,hibernate会将OtherEvent插入other.event&hibernate会将OtherLocation插入other.location(包括事件id)。 然后hibernate尝试执行以下操作:更新public.location set event_id=?位置=? 此操作失败,因为我没有对public.location的权限。如何防止它执行此更新?或者将其更改为在other.location表上进行更新

我尝试修改public.hbm.xml并更改:

<bag name="locations">
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>
其他类别:

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>

我认为解决方案非常接近。你说:

我根本无法更改数据库。我正在使用Hibernate4。我该怎么办

因此,我希望您可以更改代码和映射。如果我是对的,您可以更改代码和映射:您尝试过的设置
inverse

<bag name="locations" inverse="true">
映射应为:

<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>

那么它应该只与插入件一起工作。相反的意思是,孩子将独自完成所有的事情,所以他必须知道父母的情况。

我认为解决方案非常接近。你说:

我根本无法更改数据库。我正在使用Hibernate4。我该怎么办

因此,我希望您可以更改代码和映射。如果我是对的,您可以更改代码和映射:您尝试过的设置
inverse

<bag name="locations" inverse="true">
映射应为:

<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>

那么它应该只与插入件一起工作。相反的意思是,孩子将独自完成所有的事情,因此他必须了解家长。

你认为唯一的方法是让“孩子”(位置)了解家长(事件)吗?这感觉不是一个很好的解决方案,但如果它有效的话……我会说,从我个人的经验来看,这绝对是正确的解决方案。在
ORM
世界中,这是最典型的场景(与标准JAVA代码不同,在标准JAVA代码中,集合通常不知道所有者)。但是使用Hibernate,您将看到这是非常正常的;)你认为唯一的办法是让“孩子”(位置)知道家长(事件)吗?这感觉不是一个很好的解决方案,但如果它有效的话……我会说,从我个人的经验来看,这绝对是正确的解决方案。在
ORM
世界中,这是最典型的场景(与标准JAVA代码不同,在标准JAVA代码中,集合通常不知道所有者)。但是使用Hibernate,您将看到这是非常正常的;)
<bag name="locations" inverse="true">
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;