我是hibernate的新手。所以,我不知道该怎么做:
我有3张桌子:
表人:
@Entity
@Table(name = "ASD_PERSON")
public class AsdPerson implements Serializable {
@Id
@SequenceGenerator(name="seq_name", sequenceName="gen_id_value", allocationSize = 1)
@GeneratedValue(generator="seq_name")
@Column(name="F_PERSON_ID", nullable = false)
private Long fPersonId;
@OneToMany(mappedBy = "AsdPerson",
cascade = CascadeType.ALL,
orphanRemoval = true)
private List<AsdPersonEvent> asdPersonEventList;
... setters and getters ...
}
表事件:
@Entity
@Table(name = "ASD_EVENT")
public class AsdEvent implements Serializable {
@Id
@SequenceGenerator(name="seq_name", sequenceName="gen_id_value", allocationSize = 1)
@GeneratedValue(generator="seq_name")
@Column(name="F_EVENT_ID", nullable = false)
private Long fEventId;
@OneToMany(mappedBy = "AsdEvent",
cascade = CascadeType.ALL,
orphanRemoval = true)
private List<AsdPersonEvent> asdPersonEventList;
... setters and getters ...
}
表人事件:
@Entity
@Table(name = "ASD_PERSON_EVENT")
@IdClass(AsdPersonEventPK.class)
public class AsdPersonEvent implements Serializable {
@Id
@GenericGenerator(name = "generator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "asdPerson"))
@GeneratedValue(generator = "generator")
@Column(name="F_PERSON_ID", nullable = false, insertable = false,
updatable = false)
private Long fPersonId;
@Id
@GenericGenerator(name = "generator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "asdEvent"))
@GeneratedValue(generator = "generator")
@Column(name="F_EVENT_ID", nullable = false, insertable = false,
updatable = false)
private Long fEventId;
@ManyToOne
@JoinColumn(name = "F_PERSON_ID", insertable = false,
updatable = false)
private AsdPerson asdPerson;
@ManyToOne
@JoinColumn(name = "F_EVENT_ID", insertable = false,
updatable = false)
private AsdEvent asdEvent;
... setters and getters ...
}
当我尝试从Event表或Person表中删除相关记录时,除了大小写之外,一切都完美无缺(添加新记录,创建新对象):
...
AsdEvent ev = getService().get(115); // get record from Event table by id = 115 (for example)
ev.getAsdPersonEventList().remove(1); // delete some existing records
getService().merge(ev);
...
之后我收到错误:
删除的对象将被重新保存 级联(从中删除已删除的对象) 协会): [database.AsdPersonEvent#database.AsdPersonEventPK@12908fc]
如何使用注释或其他方法配置Hibernate以消除此错误?
答案 0 :(得分:2)
如果您有持久实体的复杂图表,我认为您需要放弃使用orphanRemoval
并使用em.remove()
手动删除您的实体。
orphanRemoval
是为简单的父子关系而设计的,其中没有父母,孩子就没有意义。如果在你的情况下,孩子可能有更好的关系,也许这对orphanRemoval
来说不是一个好例子。
答案 1 :(得分:0)
删除orphanRemoval = true