[英]Hibernate one-to-many collection deleted
I have an Object holding 3 Collections. 我有一个持有3个收藏夹的对象。 The Objects in these Collections inherite form the same Superclass.
这些集合中的对象继承相同的超类。
I use SingleTable Inheritance with @ForceDiscriminator. 我在@ForceDiscriminator中使用SingleTable继承。
The Collections are unidirectional One-To-Many. 集合是单向一对多的。
When I clear one of these Collections the other two loose their foreign key link to the holding object. 当我清除这些集合之一时,其他两个将其外键链接松开到保持对象。
I use Hibernate 3.5.3 in an WebSphere 7 (with a JPA2.0 feature pack) Container. 我在WebSphere 7(带有JPA2.0功能包)容器中使用Hibernate 3.5.3。
Entities 实体
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name="PLANUNG")
@DiscriminatorColumn(name="DISC", discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue("dummy")
@ForceDiscriminator
public abstract class Planung extends EntityBase {
@Column(name = "JAHR", nullable=false)
private int jahr;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch=FetchType.EAGER)
@JoinColumn(name="PLANUNG_ID", referencedColumnName="id")
@OrderBy("id ASC")
private List<Werte> Werte;
[...]
@Entity
@DiscriminatorValue(PlanungA.NAME)
public class PlanungA extends Planung {
public static final String NAME = "PlanungA";
}
@Entity
@DiscriminatorValue(PlanungB.NAME)
public class PlanungB extends Planung {
public static final String NAME = "PlanungB";
}
@Entity
@DiscriminatorValue(PlanungC.NAME)
public class PlanungC extends Planung {
public static final String NAME = "PlanungC";
}
---
@Entity
@DiscriminatorValue(Base.NAME)
public class Base extends AbstractBase {
public static final String NAME = "Base";
@OrderBy("jahr ASC")
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="BASE_ID", referencedColumnName="id")
private List<PlanungA> planungA;
@OrderBy("jahr ASC")
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="BASE_ID", referencedColumnName="id")
private List<PlanungB> planungB;
@OrderBy("jahr ASC")
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="BASE_ID", referencedColumnName="id")
private List<PlanungC> planungC;
[...]
Application Code 申请代码
List list = base.getPlanungA();
list.clear();
Problem 问题
Beside other statements Hibernate issues this SQL: 除了其他语句,Hibernate发出以下SQL:
update
PLANUNG
set
BASE_ID=null
where
BASE_ID=?
As a consequence all Collections (PlanungA, B, C) loose their reference to the Base object. 结果,所有集合(PlanungA,B,C)都失去了对基础对象的引用。
A Discriminator is missing (eg. AND DISC='PlanungA'). 鉴别符缺失(例如AND DISC ='PlanungA')。
What I already tried 我已经尝试过的
Any help, pointing me to similar issues etc. is greatly appreciated. 任何帮助,将我引向类似问题等,均深表感谢。
Thanks! 谢谢!
Thanks, overmeulen, for pointing me to the corresponding hibernate issue . 谢谢你,让我感到过分的冬眠问题 。 It is still an open Hibernate bug.
它仍然是一个开放的Hibernate错误。
I applied one of the solutions mentioned in the issue comments: 我应用了问题注释中提到的一种解决方案:
@OrderBy("jahr ASC")
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="BASE_ID", referencedColumnName="id")
@Where(clause="DISC='PlanungA'")
private List<PlanungA> planungA;
By inserting @Where the statement is issued by the OR Mapping as follows: 通过在@OR语句的以下位置插入@Where语句:
update
PLANUNG
set
BASE_ID=null
where
BASE_ID=?
and (
DISC='PlanungA'
)
Problem solved 问题解决了
Thanks! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.