我测试休眠这里的情况和代码:Hibernate的对象状态
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Tag tag;
// (case A)
Session session = factory.getCurrentSession();
Transaction tx = session.beginTransaction();
tag = (Tag) session.get(Tag.class, 1);
tag.setName("A");
tx.commit();
// session is automatically closed since it is current session and I am committing the transaction
// session.close();
//here the tag object should be detached
//(case B)
session = factory.getCurrentSession();
tx = session.beginTransaction();
// tag = (Tag) session.merge(tag); // I am not merging
tag.setName("B"); //changing
// session.update(tag);
tx.commit();
// session.close();
}
它不适合case B
更新(tag.setName("B")
不工作)。
然后我取消session.update(tag);
case B
,现在它工作。它应该给错误,因为对象不合并到case B
事务。
我们可以说,我们正在使用factory.getCurrentSession()
这就是为什么没有需要合并,但如果与factory.openSession();
取代它,它仍然是工作每种情况后,关闭会话(与case B
调用更新)。那么在某种意义上,我们称之为分离的对象?