通过Session.update和HibernateTemplate.merge进行Hibernate更新的区别 [英] Difference between Hibernate update by Session.update and HibernateTemplate.merge

查看:142
本文介绍了通过Session.update和HibernateTemplate.merge进行Hibernate更新的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了更新操作的类型:
第一:

  getHibernateTemplate()。execute(new HibernateCallback ){
public Object doInHibernate(Session session){
session.flush();
session.setCacheMode(CacheMode.IGNORE);
SomeObject ss =(SomeObject)session.get (SomeObject.class,id);
long next = ss.getAndIncrement();
session.update(ss);
session.flush();
return null;
}
});

其次

  SomeObject ss = loadSomeObject(); 
long next = ss.getAndIncrement();
getHibernateTemplate.merge(ss);

这两种方法都是一样的。我想知道哪一个更好更安全,为什么。谢谢。

解决方案

在第一个操作中,对象ss被附加到会话中。在第二个操作中它是分离的。所以如果你有一个附加的对象,你可以使用更新。如果你有一个分离的对象,那么使用第一个将该对象附加到对象的合并,然后执行更新

<编辑:关于附加(持久)和分离对象的信息:


$ b Hibernate定义并支持以下对象状态:



Transient - 如果一个对象刚刚使用new运算符实例化,则它是暂时的,并且它不与Hibernate Session关联。它在数据库中没有持久表示并且没有分配标识符值。如果应用程序不再拥有引用,则垃圾收集器将销毁瞬态实例。使用Hibernate Session使对象持久化(并让Hibernate处理需要为此转换执行的SQL语句)。



持久

strong> - 持久实例在数据库中有一个表示和一个标识符值。它可能只是被保存或加载,但是,它被定义在Session的范围内。 Hibernate将检测对处于持久状态的对象所做的任何更改,并在工作单元完成时将状态与数据库同步。



分离 - 分离的实例是一个对象,它是一个对象,一直持续,但其会议已经结束。当然,该对象的引用仍然有效,并且分离的实例甚至可能会在此状态下进行修改。分离的实例可以在稍后的时间点重新附加到新的会话中,使得它(和所有的修改)再次持久。此功能为需要用户思考时间的长时间运行的工作单元启用编程模型。我们称之为应用程序事务,即从用户的角度来看是一个工作单元。

I saw to types of update operation: First:

    getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) {  
            session.flush();
            session.setCacheMode(CacheMode.IGNORE);
            SomeObject ss = (SomeObject) session.get(SomeObject.class, id);
            long next = ss.getAndIncrement();
            session.update(ss);
            session.flush();
            return null;
        }
    });

and secondly

    SomeObject ss = loadSomeObject(); 
    long next = ss.getAndIncrement();
    getHibernateTemplate.merge(ss);

These two method do the same. I want to know which one is better and safe and why. Thank you.

解决方案

In the first operation the object ss is attached to the session. where as in the second operation its detached. So if you have an attached objects you can use update. If you have a detached objects then use merge which first attaches the object to the session then will do an update.

EDIT: For your information on attached(persistent) and detached objects :

Hibernate defines and supports the following object states:

Transient - an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application does not hold a reference anymore. Use the Hibernate Session to make an object persistent (and let Hibernate take care of the SQL statements that need to be executed for this transition).

Persistent - a persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded, however, it is by definition in the scope of a Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes. Developers do not execute manual UPDATE statements, or DELETE statements when an object should be made transient.

Detached - a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.

这篇关于通过Session.update和HibernateTemplate.merge进行Hibernate更新的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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