1

我在公共架构中有 2 个表事件和位置。我在从公共表继承的“其他”模式中有两个表。有 4 个类可以模仿这种设置(Event、Location、OtherEvent 和 OtherLocation)。我正在尝试使用 Hibernate 来持久保存 OtherEvent & Other Location。

每个活动都有 1+ 个地点。位置对事件没有意义。

如果我创建了一个 OtherEvent 并向它添加了一个 OtherLocation 并尝试保存它,那么休眠会将 OtherEvent 插入到 other.event 中,而休眠会将 OtherLocation 插入到 other.location 中(包括 event_id)。然后hibernate尝试做: 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 列(导致非空的 ConstraintViolation)。这是因为 Location 没有对 Event 对象的引用。

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

以下是所有相关配置: 公共类:

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
)

公共.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>

其他表:

create table other.event inherits public.event(
    other_information varchar
)

create table other.location inherits public.location(
    other_loc_information varchar
)

其他类:

public class OtherEvent extends Event{
    private String otherInformation;
    //getter & setter
}

public class OtherLocation extends Location{
    private String otherLocInformation;
    //getter & setter
}

其他.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>
4

1 回答 1

1

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

我根本无法更改数据库。我正在使用 Hibernate 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;

然后它应该只与 INSERTS 一起使用。反过来意味着,那个孩子会独自做所有的事情,所以它必须了解父母。

于 2012-11-15T15:52:00.300 回答