getHibernateTemplate().save和super.getHibernateTemplate().merge

代码1_save:

super.getHibernateTemplate().save(user);
        System.out.println(":"+user.getId());
输出1:
Hibernate: insert into user (userid, userpwd, userques, userans, usermail, integral, grade, sex, realname) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
:9


代码2_merge:
super.getHibernateTemplate().merge(user);
        System.out.println(":"+user.getId());
输出2:
Hibernate: insert into user (userid, userpwd, userques, userans, usermail, integral, grade, sex, realname) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
:0

代码3_merge:
user = (User)super.getHibernateTemplate().merge(user);
        System.out.println(":"+user.getId());
输出3:
Hibernate: insert into user (userid, userpwd, userques, userans, usermail, integral, grade, sex, realname) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
:11

当我们使用merge的时候,执行完成,我们提供的对象A还是脱管状态,hibernate或者new了一个B,或者检索到
一个持久对象B,并把我们提供的对象A的所有的值拷贝到这个B,执行完成后B是持久状态,而我们提供的A还是托管状态


继续看代码:
super.getHibernateTemplate().save(user);
        user.setUserans("杭州BST");
        System.out.println(":"+user.getId());输出:
Hibernate: insert into user (userid, userpwd, userques, userans, usermail, integral, grade, sex, realname) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
:13
Hibernate: update user set userid=?, userpwd=?, userques=?, userans=?, usermail=?, integral=?, grade=?, sex=?, realname=? where id=?


super.getHibernateTemplate().merge(user);
        user.setUserans("杭州BST");
        System.out.println(":"+user.getId());

-------------------------
Hibernate: insert into user (userid, userpwd, userques, userans, usermail, integral, grade, sex, realname) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
:0




其实,我们也可以这样显示修改状态。
user = (User)super.getHibernateTemplate().merge(user);
        user.setUserans("杭州BST");
        System.out.println(":"+user.getId());

--------------------

Hibernate: insert into user (userid, userpwd, userques, userans, usermail, integral, grade, sex, realname) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
:16
Hibernate: update user set userid=?, userpwd=?, userques=?, userans=?, usermail=?, integral=?, grade=?, sex=?, realname=? where id=?
转载:
[url]http://www.blogjava.net/beijing2008/articles/317924.html[/url]

你可能感兴趣的:(getHibernateTemplate().save和super.getHibernateTemplate().merge)