hibernate session三种状态

hibernate session

hibernate session

Session对象是有生命周期的,它以Transaction对象的事务开始和结束边界

session就像数据库连接池的一个连接,用于操作在hibernate里面操作对象,不用于保存东西

 

save      并没保存到数据库中,可以自动设置id在session中保存一份对象的缓存,提高访问速度(不用每次都访问数据库)     persistent状态

/**
     * Persist the given transient instance, first assigning a generated identifier. (Or
     * using the current value of the identifier property if the <tt>assigned</tt>
     * generator is used.)  This operation cascades to associated instances if the
     * association is mapped with <tt>cascade="save-update"</tt>.
     *
     * @param object a transient instance of a persistent class
     * @return the generated identifier
     * @throws HibernateException
     */
    public Serializable save(String entityName, Object object) throws HibernateException;

 

commit      发出sql语句 ,将关闭session,对象引用不在session中     detached状态

                  相当于一个缓存和数据库的一个同步

                   commit时调用了flush方法

(ps:commit后,session对象消失,如果该对象引用到另一个事物中,如果修改对象属性,事物提交时不会将该游离态对象同步,需手动调用saveOrUpdate方法,手动调用如果session中存在另一个id一样的session对象,将报hibernate session id冲突问题,merge可以解决,但这种不规范,易忽视的代码不建议这么干)

delete      对象必须有id号,也就是只能删除persistent和detached状态的对象

load      将数据库记录转化为对象     Teacher t = (Teacher)session.load(Teacher. class, 1);   并没发sql语句,只是一个代理对象,在需要用的时候发sql语句

比如:session.getTransaction().commit();

         System. out.println(t.getClass());

         System. out.println(t.getBirthday());

     commit后,如果commit前没有使用过对象t,那么之后使用将报错,commit后session中的对象缓存将不存在,所以不能用对象t

get   将数据库记录转化为对象 立即发sql语句将记录封装成对象

get和load都是先查缓存 clear可以强制清除缓存

saveOrUpdate

/**
     * Either {@link #save(String, Object)} or {@link #update(String, Object)}
     * the given instance, depending upon resolution of the unsaved-value checks
     * (see the manual for discussion of unsaved-value checking).
     * <p/>
     * This operation cascades to associated instances if the association is mapped
     * with <tt>cascade="save-update"</tt>.
     *
     * @see Session#save(String,Object)
     * @see Session#update(String,Object)
     * @param object a transient or detached instance containing new or updated state
     * @throws HibernateException
     */
    public void saveOrUpdate(String entityName, Object object) throws HibernateException;


update   更新detached或者persistent对象或者transient 需有主键

          update默认更新所有字段,效率低,可设置字段不参与更新,或xml里面设置有变更时更新

          persistent对象设置不同字段在commit时就会更新

          当和数据库相同是不会更新

/**
     * Update the persistent instance with the identifier of the given detached
     * instance. If there is a persistent instance with the same identifier,
     * an exception is thrown. This operation cascades to associated instances
     * if the association is mapped with <tt>cascade="save-update"</tt>.
     *
     * @param object a detached instance containing updated state
     * @throws HibernateException
     */
    public void update(String entityName, Object object) throws HibernateException;

merge      先从数据库load记录比较和对象的不同

/**
     * Copy the state of the given object onto the persistent object with the same
     * identifier. If there is no persistent instance currently associated with
     * the session, it will be loaded. Return the persistent instance. If the
     * given instance is unsaved, save a copy of and return it as a newly persistent
     * instance. The given instance does not become associated with the session.
     * This operation cascades to associated instances if the association is mapped
     * with <tt>cascade="merge"</tt>.<br>
     * <br>
     * The semantics of this method are defined by JSR-220.
     *
     * @param object a detached instance with state to be copied
     * @return an updated persistent instance
     */
    public Object merge(Object object) throws HibernateException;

 

createQuery hql语句返回Query对象

clear      清除缓存

flush      强制同步缓存和数据库内容

session用于管理事务增删改查

Session创建:

//当上下文里面没session则创建session,有则不创建

        Session session = sf.getCurrentSession();

                    最后不需加session.close();

     永远打开新的session

                    Session session = sf.openSession();

                     最后需加session.close();

session.commit()之后session将被关闭(getcurr

getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭

and 必须设定current_session_context_class

转载于:https://my.oschina.net/yugj/blog/521926

相关文章: