在hibernate的onPostUpdateCollection事件中获取旧值 [英] Get old values in collection at onPostUpdateCollection event in hibernate

查看:456
本文介绍了在hibernate的onPostUpdateCollection事件中获取旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用hibernate 4.2进行持久性存储。我正在实现hibernate事件侦听器以在特定对象被修改时获取通知。
试图在hibernate中实现 PostUpdateEventListener 事件,但它在更新集合值时不触发方法。
当前正在执行 PostCollectionUpdateEventListener ,这会在收集更新时触发方法。



该类如下 p>

  public class Employee {
private int id;
私人字符串名称;
私人套餐<地址>地址;

//实现所有getter和setter。
}

公共类地址{
private int id;
私人字符串街道;
私人字符串城市;

//实现所有getter和setter。



$ b我已经将映射作为xml文件使用所有映射和以下集合映射实现 p>

在Employee.hbm.xml中

 < hibernate-mapping> 
< class name =Employee>
...所有映射
< set name =addressesinverse =truecascade =all-delete-orphan>
< key column =Emp_id/>
<一对多等级=地址/>
< / set>
< / hibernate-mapping>

地址.hbm.xml文件已正确实施。



在hibernate事件监听器中

  public void onPostUpdateCollection(PostCollectionUpdateEvent event){
Object obj = event。 getAffectedOwnerOrNull();
//这给了我更新的值。

我现在想要获取将要删除的旧集合值的代码。
}

我试过以下行:

  PersistentCollection collection = event.getCollection(); 
//这给出地址的新更新集合值

我在PersistentCollection Serializable getStoredSnapshot()但它给出空值。

以任何方式,如果我可以得到旧的集合值,那么请帮我。
我插入新的地址值,以便在Employee类对象上触发 onPostUpdateCollection()事件方法。



我的问题是:
如何检索旧集合的值?
试图从两天内获得旧的价值,任何帮助将非常感激。
在此先感谢。

解决方案

在PostCollectionUpdateEventListener中,无法获取旧的集合值。
我使用PreCollectionUpdateEventListener类来获取旧的集合值,如下所示:

  public void onPreUpdateCollection(PreCollectionUpdateEvent event){
PersistentCollection collection = event.getCollection();
HashMap snapshot =(HashMap)collection.getStoredSnapshot();
//设置值也存储为具有与设置值相同的键和值的映射值
Set< Map.Entry> set = snapshot.entrySet();
//现在这个集合包含更新之前的旧集合值的键值

code $ pre

I am using hibernate 4.2 for persistent storage. I am implementing hibernate event listeners for getting the notification when particular object is modified. Tried Implementing PostUpdateEventListener event in hibernate but it does not trigger method at the time of updating collection values. Currently implementing PostCollectionUpdateEventListener which triggers method when collection is updated.

The class is as follows

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.
}

I have implemented mapping as xml files with all mapping and following set mapping

In 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>

The Address .hbm.xml file is implemented correctly.

In hibernate event listener

public void onPostUpdateCollection(PostCollectionUpdateEvent event) {
  Object obj = event.getAffectedOwnerOrNull();
  //this gives me updated values.

  I want now code to get old collection values which going to be deleted.
}

I have tried the following line

PersistentCollection collection = event.getCollection();
// this gives new update collection values of addresses

I have seen method in PersistentCollection Serializable getStoredSnapshot() but it gives empty values.

In any way if i can get the old collection values then please help me. I am inserting new value of addresses so that it triggers onPostUpdateCollection() event method on Employee class object.

My Question is : How can i retrieve old values of collection? Trying to get old values from couple of days, any help will be very much appreciated. Thanks in advance.

解决方案

In PostCollectionUpdateEventListener there is no way to get old collection values. I used PreCollectionUpdateEventListener class to get old collection values as follows

public void onPreUpdateCollection(PreCollectionUpdateEvent event) {
  PersistentCollection collection = event.getCollection();
  HashMap snapshot = (HashMap) collection.getStoredSnapshot();
  //set values are also stored as map values with same key and value as set value
  Set<Map.Entry> set = snapshot.entrySet();
  //Now this set contains key value of old collection values before update
}

这篇关于在hibernate的onPostUpdateCollection事件中获取旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆