gpt4 book ai didi

java - Hibernate一对多集合删除

转载 作者:行者123 更新时间:2023-11-30 11:35:37 24 4
gpt4 key购买 nike

我有一个包含 3 个集合的对象。这些集合中的对象继承形成相同的父类(super class)。

我将 SingleTable 继承与 @ForceDiscriminator 结合使用。

集合是单向一对多的。

当我清除这些集合中的一个时,另外两个会失去与持有对象的外键链接。

我在 WebSphere 7(带有 JPA2.0 功能包)容器中使用 Hibernate 3.5.3。

实体

@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;

[...]

应用代码

List list = base.getPlanungA();
list.clear();

问题

除其他语句外,Hibernate 发出此 SQL:

update
PLANUNG
set
BASE_ID=null
where
BASE_ID=?

因此,所有集合(PlanungA、B、C)都失去了对 Base 对象的引用。

缺少鉴别器(例如 AND DISC='PlanungA')。

我已经尝试过的

  • 我已经升级到 Hibernate 3.6.10.Final(只是为了尝试)。它没有解决此行为。
  • 搜索了世界......

非常感谢任何帮助,指出类似问题等。

谢谢!

最佳答案

谢谢 overmeulen,为我指出相应的 hibernate issue .它仍然是一个开放的 Hibernate 错误。

我应用了问题评论中提到的解决方案之一:

@OrderBy("jahr ASC")
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="BASE_ID", referencedColumnName="id")
@Where(clause="DISC='PlanungA'")
private List<PlanungA> planungA;

通过插入 @Where OR 映射发出如下语句:

update
PLANUNG
set
BASE_ID=null
where
BASE_ID=?
and (
DISC='PlanungA'
)

问题解决了

谢谢!

关于java - Hibernate一对多集合删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15001000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com