我使用 hibernate 4.2 进行持久存储。我正在实现休眠事件侦听器,以便在修改特定对象时获取通知。尝试PostUpdateEventListener在休眠中实现事件,但在更新集合值时不会触发方法。目前正在实现PostCollectionUpdateEventListener集合更新时触发的方法。
班级如下
public class Employee {
private int id;
private String name;
private Set<Address> addresses;
//all getters and setters are implemented.
}
public class Address {
private int id;
private String street;
private String city;
//all getters and setters are implemented.
}
Run Code Online (Sandbox Code Playgroud)
我已将映射实现为 xml 文件,其中包含所有映射和以下设置映射
在 Employee.hbm.xml 中
<hibernate-mapping>
<class name="Employee">
... all mappings
<set name="addresses" inverse="true" cascade="all-delete-orphan">
<key column="Emp_id"/>
<one-to-many class="Address"/>
</set>
</hibernate-mapping>
Run Code Online (Sandbox Code Playgroud)
地址 .hbm.xml 文件已正确实现。
在休眠事件监听器中
public void onPostUpdateCollection(PostCollectionUpdateEvent event) { …Run Code Online (Sandbox Code Playgroud)