【发布时间】:2015-02-10 16:00:59
【问题描述】:
我正在努力让 Eclipselink 级联保持关系:
@Entity
class Notification {
@Id
@UuidGenerator(name="UUID_GEN")
@GeneratedValue(generator="UUID_GEN")
@Column(name = "NOTIFICATION_ID")
private UUID id;
@OneToOne(cascade = ALL, fetch = FetchType.EAGER)
@JoinFetch(JoinFetchType.INNER)
@PrimaryKeyJoinColumn(name="NOTIFICATION_ID", referencedColumnName="NOTIFICATION_ID")
private Notificator notificator;
...
}
@Entity
class Notificator {
@Id
@Column(name="NOTIFICATION_ID")
private UUID id;
...
}
所以当我尝试持久化一个 Notification 对象时,Eclipselink 未能持久化封闭的 Notificator 对象,因为 Notificator.id 没有设置,所以发生了约束失败。
在第二次尝试中,我尝试使用@MapId 注释:
@Entity
class Notification {
@Id
@UuidGenerator(name="UUID_GEN")
@GeneratedValue(generator="UUID_GEN")
@Column(name = "NOTIFICATION_ID")
private UUID id;
@OneToOne
@MapsId
private Notificator notificator;
...
}
@Entity
class Notificator {
@Id
@Column(name="NOTIFICATION_ID")
private UUID id;
@OneToOne
PrimaryKeyJoinColumn(name="NOTIFICATION_ID", referencedColumnName="NOTIFICATION_ID")
private Notification notification; // didn't need to have it here, but fine.
...
}
这样,我什至无法带上EntityManager。它抛出这个错误:
异常描述:必须为 序列号字段。
有没有办法让这个级联工作,所以我不需要分别保留这两个实体?我试图避免这种情况,因为有几个其他实体共享同一个 ID,所以就目前而言,这将是一项肮脏的工作。
谢谢
【问题讨论】:
标签: hibernate jpa eclipselink jpa-2.0