Hibernate检测一个对象是否具有并发持久性 [英] Hibernate detecting if an object is transient with concurrent persistence

查看:67
本文介绍了Hibernate检测一个对象是否具有并发持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



通常我通过检查id是否存在已设置,但我担心这不是线程安全的,我有另一个线程保存对象,它可能没有设置id,但Hibernate已经在保存对象的过程。

如何以线程安全的方式执行此操作?



谢谢。

  public void updateIfNotNew(PersistentObject a){
//如果另一个对象正在保存......此更新将不会持续
if( a.id!= null){
session.update(a);
}
}


解决方案

I建议您查找接口 org.hibernate.Session ,您可以在其中找到包含的方法,该方法将查看与其关联的给定对象与该会话(持久),如果不是,并不意味着它是分离的,因为它可以与其他会话相关联(如果有的话)。


I need to detect if an object is transient..if it is not I want to update via Hibernate.

Usually I do this by checking if the id has been set but I am worried this is not thread safe and I have another thread which is saving the object and its possible that the id is not set but the Hibernate is already in the process of saving the object.

How can I do this in a thread-safe way?

Thanks.

public void updateIfNotNew(PersistentObject a){
//if another object is in the process of saving....this update will not get persisted
    if (a.id!=null){  
        session.update(a);
     }
}

解决方案

I recommend you to look for the interface org.hibernate.Session there you can find the method contains, this method will look if the given object it's associated with that session (persistent), if not, doesn't mean that it's detached, because it could be associated with other session if any.

这篇关于Hibernate检测一个对象是否具有并发持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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