在我的应用程序中,一个hibernate操作就像这样.应用程序使用请求中的新值更新父实体,并删除所有现有(先前插入的)子实体并插入新的子记录.
我正在使用冬眠DELETE_ORPHAN,如下所示.
当我这样做时,我得到以下异常:
org.hibernate.HibernateException:拥有实体实例不再引用cascade ="all-delete-orphan"的集合:com.childs
我看到了与问题类似的线程,我试图在这些线程中应用解决方案.但那没用
我的父实体
public class Parent implements Serializable {
@Column(name = "PARENT_ID")
@Basic(fetch = FetchType.EAGER)
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq")
@SequenceGenerator(name = "seq", sequenceName = "seq")
private Integer parentId; //primary key of parent
.......
........
//mapping to child entity
@OneToMany(mappedBy = "parent", cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Child> childs;
................
...............
}
Run Code Online (Sandbox Code Playgroud)
子实体具有组合键,并且具有PK实体,如下所示
public class ChildPK implements Serializable {
/** The Constant serialVersionUID. */
private static final …Run Code Online (Sandbox Code Playgroud) 我在通过级联删除孤儿删除另一个实体时遇到问题.当我清除相关的集合集合时它会起作用,但是当我将集合集合置为空时它不起作用.让我详细解释一下.配置代码段:
<class name="com.sample.CategoriesDefault" table="cats">
<id name="id" column="id" type="string" length="40" access="property">
<generator class="assigned" />
</id>
<version name="version" column="objVrs" unsaved-value="negative"/>
<set name="bla" lazy="false" cascade="all-delete-orphan" inverse="true">
<key column="idCats" not-null="true"/>
<one-to-many class="com.sample.BLA"/>
</set>
<class name="com.sample.BLA" table="blaTEST">
<id name="id" column="id" type="string" length="40" access="property">
<generator class="assigned" />
</id>
<version name="version" column="objVrs" unsaved-value="negative"/>
<property name="bla" type="string" column="bla"/>
<many-to-one name="parent" class="com.sample.CategoriesDefault" column="idCats" not-null="true"/>
</class>
Run Code Online (Sandbox Code Playgroud)
我的示例代码:
Categories cats = new CategoriesDefault();
final Set<BLA> col = new HashSet<BLA>();
col.add(new BLA(cats));
cats.setBla(col);
cats.saveOrupdate(); // will update/insert it in the db.
Run Code Online (Sandbox Code Playgroud)
以下工作正常,即:所有集合项都从数据库中移出. …
我在它们之间有PolicyDO和PolicyDocumentDO.relation如下
PolicyDO.hbm.xml
<bag name="listPolicyDocumentDOList" cascade="all-delete-orphan" lazy="false" inverse="true">
<key column="POLICYSEQ" />
<one-to-many class="dataobjects.policy.PolicyDocumentDO" />
Run Code Online (Sandbox Code Playgroud)
PolicyDO.java
protected List<PolicyDocumentDO> policyDocumentDOList = new ArrayList<PolicyDocumentDO>();
public java.util.List<PolicyDocumentDO> getListPolicyDocumentDOList() {
return this.policyDocumentDOList;
}
public void setListPolicyDocumentDOList(java.util.List<PolicyDocumentDO> list) {
policyDocumentDOList.clear();
policyDocumentDOList = list;
}
PolicyDocumentDO.hbm.xml
<many-to-one name="parentGuidObj" class="dataobjects.policy.PolicyDO" not-null="true" >
<column name="POLICYSEQ" />
</many-to-one>
Run Code Online (Sandbox Code Playgroud)
我什么时候尝试从数据库中查询某些内容,如下所示
session = sessionFactory.openSession();
Query query = session.createQuery(strBuff.toString());
List listQuery = query.list();
Run Code Online (Sandbox Code Playgroud)
我得到以下错误
org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:
Run Code Online (Sandbox Code Playgroud)
dataobjects.policy.PolicyDO.listPolicyDocumentDOList
所以在谷歌搜索后,我在PolicyDO中设置listPolicyDocumentDOList时做了以下更改
public void setListPolicyDocumentDOList(java.util.List<PolicyDocumentDO> list) …Run Code Online (Sandbox Code Playgroud) 所以我有一个包含 DateActiveScheduleItem 列表的日程表对象。我从数据库中获取日程表,从列表中删除一个 DateActiveScheuleItem,并使用 Hibernate CrudRepository 中的 .save() 保存它。
nested exception is org.springframework.orm.jpa.JpaSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: haughton.daniel.ShoutLoud.model.schedule.Schedule.dateActiveScheduleItems; nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: haughton.daniel.ShoutLoud.model.schedule.Schedule.dateActiveScheduleItems] with root causeorg.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: haughton.daniel.ShoutLoud.model.schedule.Schedule.dateActiveScheduleItems
Run Code Online (Sandbox Code Playgroud)
时间表.java
@Entity
public class Schedule {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "user_id")
private …Run Code Online (Sandbox Code Playgroud)