Hibernate Pojo的三态分别为transient(瞬时态),persistent(持久态),detached(游离态)
1、官方给出的三态与Session的关系如下:
transient: never persistent, not associated with any Session
persistent: associated with a unique Session
detached: previously persistent, not associated with any Session
2、三种状态间相互转换关系,及他们在数据库、session中的状态如下:
a.当我们new一个pojo时,它处于瞬时态,此时与session、数据库均无任何关联。
b.此后,我们获得session并开启hibernate事务,调用save(),persist(),saveOrUpdate()方法,将pojo转变为持久态,此时session中存有这个pojo,但直到transaction.commit()被调用时,sql语句才会执行,此后数据库中也才有此条数据。
c.但当commit()并且session.close()执行过后,pojo就变为了游离态,也就是说,数据库中有该记录,但session中不再有。
d.持久化状态的实例,也可以重新变成瞬时态,那就是调用delete()方法。
e.通过get()或load()方法得到的pojo是持久态的。
f.游离态的pojo可以通过update(),saveOrUpdate(),lock()和replicate()方法重新成为持久态。
g.调用merge()方法后,pojo维持原态,但内容会被更新到数据库