[英]Hibernate: delete records from association table with foreign keys
I'm new in hibernate. 我是hibernate的新手。 So, I don't know how to do this:
所以,我不知道该怎么做:
I have 3 tables: 我有3张桌子:
Table Person: 表人:
@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 ...
}
Table Event: 表事件:
@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 ...
}
Table Person-Event: 表人事件:
@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 ...
}
Everything works perfectly (adding new records, creating new objects) except the case, when I try to delete associated records from Event table or Person table: 当我尝试从“事件”表或“人”表中删除关联的记录时,除以下情况外,一切工作正常(添加新记录,创建新对象):
...
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);
...
After that I get error: 之后我收到错误:
deleted object would be re-saved by cascade (remove deleted object from associations): [database.AsdPersonEvent#database.AsdPersonEventPK@12908fc]
删除的对象将通过级联重新保存(从关联中删除已删除的对象):[database.AsdPersonEvent#database.AsdPersonEventPK@12908fc]
How to configure Hibernate with annotations or some other way to get rid of this error? 如何用注释或其他方法配置Hibernate来消除此错误?
If you have a complex graph of persistent entities, I think you need to give up using orphanRemoval
and remove your entities manually using em.remove()
. 如果你有一个复杂的持久化实体图,我认为你需要放弃使用
orphanRemoval
并使用em.remove()
手动删除你的实体。
orphanRemoval
is designed for simple parent-child relationships, where child doesn't make sense without parent. orphanRemoval
是为简单的父子关系而设计的,如果没有父母,孩子就没有意义。 If in your case child may have ohter relationships, perhaps it's not a good case for orphanRemoval
. 如果在你的情况下,孩子可能有更好的关系,也许这对
orphanRemoval
来说不是一个好例子。
删除orphanRemoval = true
再试一次
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.