您的映射(简化)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="br.com._3988215.model.domain">
<class name="Parent" table="PARENT">
<id name="id">
<generator class="native"/>
</id>
<bag cascade="all,delete-orphan" name="childList">
<key column="PARENT_ID" not-null="false"/>
<one-to-many class="Child"/>
</bag>
</class>
<class name="Child" table="CHILD">
<id name="id" column="CHILD_ID">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
生产
PARENT
ID
CHILD
CHILD_ID
PARENT_ID
根据你说的话
我希望能够让Hibernate检测到子集合已从父对象中删除,并在更新父对象时从数据库中删除子表中的行
类似的东西
Parent parent = session.get(...);
parent.getChildren().clear();
session.update(parent);
您说它工作正常,因为您有一个附加的父实例
现在让我们看看下面的一个(注意 Assert.assertNull(second))
public class WhatYouWantTest {
private static SessionFactory sessionFactory;
private Serializable parentId;
private Serializable firstId;
private Serializable secondId;
@BeforeClass
public static void setUpClass() {
Configuration c = new Configuration();
c.addResource("mapping.hbm.3988215.xml");
sessionFactory = c.configure().buildSessionFactory();
}
@Before
public void setUp() throws Exception {
Parent parent = new Parent();
Child first = new Child();
Child second = new Child();
Session session = sessionFactory.openSession();
session.beginTransaction();
parentId = session.save(parent);
firstId = session.save(first);
secondId = session.save(second);
parent.getChildList().add(first);
parent.getChildList().add(second);
session.getTransaction().commit();
session.close();
}
@Test
public void removed_second_from_parent_remove_second_from_database() {
Parent parent = new Parent();
parent.setId((Integer) parentId);
Child first = new Child();
first.setId((Integer) firstId);
/**
* It simulates the second one has been removed
*/
parent.getChildList().add(first);
Session session = sessionFactory.openSession();
session.beginTransaction();
session.update(parent);
session.getTransaction().commit();
session.close();
session = sessionFactory.openSession();
session.beginTransaction();
Child second = (Child) session.get(Child.class, secondId);
Assert.assertNull(second);
session.getTransaction().commit();
session.close();
}
}
不幸的是,测试没有通过。您可以???做什么
休眠引用说
扩展(或长)会话 - 在提交数据库事务后,休眠会话可能会与底层 JDBC 连接断开连接,并在发生新的客户机请求时重新连接。此模式称为会话会话,甚至不需要重新附加。自动版本控制用于隔离并发修改,通常不允许自动刷新会话,而是显式刷新会话。
免责声明:我没有任何使用长时间对话的场景。Java EE 有状态会话 Bean 支持长时间运行的会话。但它支持JPA(不是Hibernate)
或者,您可以创建一个替代映射,使子元素成为复合元素。由于它的生命周期取决于父对象,因此您可以依靠复合元素来获得所需的内容
创建一个名为 AlternativeParent 的类,该类扩展了 Parent
public class AlternativeParent extends Parent {}
现在它的映射(注意子元素作为复合元素,而不是普通的@Entity)
<class name="AlternativeParent" table="PARENT">
<id name="id">
<generator class="native"/>
</id>
<bag name="childList" table="CHILD">
<key column="PARENT_ID" not-null="false"/>
<composite-element class="Child">
<property column="CHILD_ID" name="id"/>
</composite-element>
</bag>
</class>
现在在子类中实现一个方便的等式方法
public boolean equals(Object o) {
if (!(o instanceof Child))
return false;
Child other = (Child) o;
// identity equality
// Used by composite elements
if(getId() != null) {
return new EqualsBuilder()
.append(getId(), other.getId())
.isEquals();
} else {
// object equality
}
}
如果我重构上面显示的测试用例(现在使用替代父母代替)
@Test
public void removed_second_from_parent_remove_second_from_database() {
AlternativeParent parent = new AlternativeParent();
parent.setId((Integer) parentId);
Child first = new Child();
first.setId((Integer) firstId);
/**
* It simulates the second one has been removed
*/
parent.getChildList().add(first);
Session session = sessionFactory.openSession();
session.beginTransaction();
session.update(parent);
session.getTransaction().commit();
session.close();
session = sessionFactory.openSession();
session.beginTransaction();
Child second = (Child) session.get(Child.class, secondId);
Assert.assertNull(second);
session.getTransaction().commit();
session.close();
}
我看到一个绿色条