我在公共架构中有 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>