码迷,mamicode.com
首页 > Web开发 > 详细

02-Hibernate实体类的三种状态

时间:2015-03-12 15:02:08      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

一.Hibernate的实体类有三种状态

Transient:瞬时状态,不被hibernate session管理,而且实体数据在数据库不存在

Persistent:持久化状态,被session管理,此时的对象被放在session的缓存中,实体对象存在数据库中。

Detached:离线状态,实体对象在数据库中有数据与之对应,但是不被session管理

注意:被session所管理的实体对象,当对象的属性发生改变,在提交事务的时候会自动更新改变后的属性到数据库中。

下图是hibernate试题对象三种状态的转化

技术分享

二.三种状态转换代码演示

/**

     * 调用Save()方法。对象由transient状态转变为persistent状态

     */

    @Test

    public void testTransicentSave() {

        Session session=null;

        try {

            session=HibernateUtil.openSession();

            session.beginTransaction();

            User u=new User();

            u.setNickName("steven");

            u.setPassword("000");

            u.setUserName("xinchen");

            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

            u.setBorn(sdf.parse("1987-01-01"));

            //在调用save方法之前,此时的utransient状态

            //调用save方法后,upersistent状态,对象受session的管理

            session.save(u);

            //更改u对象,因为upersistent状体,所以commit的时候会发出两条sql语句 insertupdate

            u.setUserName("xchen8");

            session.getTransaction().commit();

        } catch (Exception e) {

            e.printStackTrace();

            if(session!=null) session.getTransaction().rollback();

        } finally{

            HibernateUtil.closeSession(session);

        }

    }

    

    /**

     * 调用SaveOrUpdate()方法。对象由transient状态转变为persistent状态

     */

    @Test

    public void testTransicentSaveOrUpdate() {

        Session session=null;

        try {

            session=HibernateUtil.openSession();

            session.beginTransaction();

            User u=new User();

            u.setNickName("超人");

            u.setPassword("000");

            u.setUserName("superman");

            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

            u.setBorn(sdf.parse("1988-01-01"));

            //在调用save方法之前,此时的utransient状态

            //调用save方法后,upersistent状态,对象受session的管理

            session.saveOrUpdate(u);

            //更改u对象,因为upersistent状体,所以commit的时候会发出两条sql语句 insertupdate

            u.setUserName("xin.chen");

            session.getTransaction().commit();

        } catch (Exception e) {

            e.printStackTrace();

            if(session!=null) session.getTransaction().rollback();

        } finally{

            HibernateUtil.closeSession(session);

        }

    }

    

    /**

     * 测试persistent状态下update多次没有意义

     */

    @Test

    public void testPersistent01() {

        Session session=null;

        try {

            session=HibernateUtil.openSession();

            session.beginTransaction();

            User u=new User();

            u.setNickName("钢铁侠");

            u.setPassword("000");

            u.setUserName("iron-man");

            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

            u.setBorn(sdf.parse("1908-01-01"));

            //在调用save方法之前,此时的utransient状态

            //调用save方法后,upersistent状态,对象受session的管理

            session.save(u);

            u.setUserName("Iron Man");

            //对象已经是persistent状体,调用update没有意义

            session.update(u);

            u.setPassword("333");

            session.update(u);

            //只会发出两条sqlinsertupdate

            session.getTransaction().commit();

        } catch (Exception e) {

            e.printStackTrace();

            if(session!=null) session.getTransaction().rollback();

        } finally{

            HibernateUtil.closeSession(session);

        }

    }

        

        /**

         * 测试persistent状态下update多次没有意义

         */

        @Test

        public void testPersistent02() {

            Session session=null;

            try {

                session=HibernateUtil.openSession();

                session.beginTransaction();

                User u=new User();

                u.setNickName("绿巨人");

                u.setPassword("000");

                u.setUserName("Green Man");

                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

                u.setBorn(sdf.parse("1908-01-01"));

                //在调用save方法之前,此时的utransient状态

                //调用save方法后,upersistent状态,对象受session的管理

                session.save(u);

                //对象已经是persistent状体,调用update没有意义

                session.update(u);

                session.update(u);

                //只会发出一条sqlinsert

                session.getTransaction().commit();

            } catch (Exception e) {

                e.printStackTrace();

                if(session!=null) session.getTransaction().rollback();

            } finally{

                HibernateUtil.closeSession(session);

            }

    }

        

        /**

         * 测试persistent状态,通过load方法变成persistent状态

         */

        @Test

        public void testPersistent03() {

            Session session=null;

            try {

                session=HibernateUtil.openSession();

                session.beginTransaction();

                //调用load方法后,upersistent状态,对象受session的管理

                User u=(User)session.load(User.class, 1L);

                u.setUserName("CX");

                //会发出两条条sqlselect,update

                session.getTransaction().commit();

            } catch (Exception e) {

                e.printStackTrace();

                if(session!=null) session.getTransaction().rollback();

            } finally{

                HibernateUtil.closeSession(session);

            }

    }

      

        

        /**

         * 测试persistent状态,通过load方法变成persistent状态

         */

        @Test

        public void testPersistent04() {

            Session session=null;

            try {

                session=HibernateUtil.openSession();

                session.beginTransaction();

                //调用load方法后,upersistent状态,对象受session的管理

                User u=(User)session.load(User.class, 1L);

                session.getTransaction().commit();

                //u此时为离线状态,只会发一条select sql

                u.setUserName("CXddd");

                //会发出两条条sqlselect,update

            } catch (Exception e) {

                e.printStackTrace();

                if(session!=null) session.getTransaction().rollback();

            } finally{

                HibernateUtil.closeSession(session);

            }

    }

        

        /**

         * 测试persistent状态,通过load方法变成persistent状态

         */

        @Test

        public void testPersistent05() {

            Session session=null;

            try {

                session=HibernateUtil.openSession();

                session.beginTransaction();

                //调用load方法后,upersistent状态,对象受session的管理

                User u=(User)session.load(User.class, 1L);

                session.getTransaction().commit();

                //u此时为离线状态,只会发一条select sql

                session.beginTransaction();

                //upersistent状态

                u.setUserName("CX11");

                //会发出两条条sqlselect,update

                session.getTransaction().commit();

            } catch (Exception e) {

                e.printStackTrace();

                if(session!=null) session.getTransaction().rollback();

            } finally{

                HibernateUtil.closeSession(session);

            }

    }

      

        /**

         * 测试persistent状态,通过load方法变成persistent状态

         * 调用clear方法,变为detached状态

         */

        @Test

        public void testPersistent06() {

            Session session=null;

            try {

                session=HibernateUtil.openSession();

                session.beginTransaction();

                //调用load方法后,upersistent状态,对象受session的管理

                User u=(User)session.load(User.class, 1L);

                //upersistent状态

                u.setUserName("CX11ddd");

                //调用clear方法后,u为离线状态,所以只发一条select sql

                session.clear();

                session.getTransaction().commit();

            } catch (Exception e) {

                e.printStackTrace();

                if(session!=null) session.getTransaction().rollback();

            } finally{

                HibernateUtil.closeSession(session);

            }

    }        

        

    /**

     * 测试detached状态,

     */

    @Test

    public void testDetached01() {

        Session session=null;

        try {

            session=HibernateUtil.openSession();

            session.beginTransaction();

            //udetached状态,因为数据库中有id=1的数据

            User u=new User();

            u.setId(1L);

            u.setUserName("测试detached");

            //调用save后,会根据id生成规则生成一条persistent的对象

            session.save(u);

            session.getTransaction().commit();

        } catch (Exception e) {

            e.printStackTrace();

            if(session!=null) session.getTransaction().rollback();

        } finally{

            HibernateUtil.closeSession(session);

        }

    }

    

    /**

     * 测试detached状态,

     */

    @Test

    public void testDetached02() {

        Session session=null;

        try {

            session=HibernateUtil.openSession();

            session.beginTransaction();

            //udetached状态,因为数据库中有id=1的数据

            User u=new User();

            u.setId(4L);

            u.setUserName("测试detached");

            //调用update后,u变成persistent的对象

            session.update(u);

            System.out.println(u.getPassword());

            u.setUserName("hhhh哦哦哦");

            session.update(u);

            //只发出一条update

            session.getTransaction().commit();

        } catch (Exception e) {

            e.printStackTrace();

            if(session!=null) session.getTransaction().rollback();

        } finally{

            HibernateUtil.closeSession(session);

        }

    }    

    

    /**

     * 测试detached状态,

     */

    @Test

    public void testDetached03() {

        Session session=null;

        try {

            session=HibernateUtil.openSession();

            session.beginTransaction();

            //udetached状态,因为数据库中有id=1的数据

            User u=new User();

            u.setId(1L);

            u.setUserName("测试detached");

            //调用delete后,u变成transient的对象

            session.delete(u);

            u.setUserName("hhhh哦哦哦11");

            //只发出一条delete

            session.getTransaction().commit();

        } catch (Exception e) {

            e.printStackTrace();

            if(session!=null) session.getTransaction().rollback();

        } finally{

            HibernateUtil.closeSession(session);

        }

    }

    

    /**

     * 测试detached状态,

     */

    @Test

    public void testDetached04() {

        Session session=null;

        try {

            session=HibernateUtil.openSession();

            session.beginTransaction();

            //udetached状态,因为数据库中有id=1的数据

            User u1=(User)session.load(User.class, 2L);

            System.out.println(u1.getUserName());

            User u2=new User();

            u2.setId(2L);

            u2.setUserName("d333");

            //此时u2将会变成持久化状态,在session的缓存中就存在了两份同样的对象,session中不能存在两份拷贝,否则会抛出异常

//            session.saveOrUpdate(u2);

            //merge方法会判断session中是否已经存在同一个对象,如果存在就将两个对象合并

            session.merge(u2);

            session.getTransaction().commit();

        } catch (Exception e) {

            e.printStackTrace();

            if(session!=null) session.getTransaction().rollback();

        } finally{

            HibernateUtil.closeSession(session);

        }

    }    

 

三.源代码in Github

查看源代码

02-Hibernate实体类的三种状态

标签:

原文地址:http://www.cnblogs.com/supermanchenxin/p/4332247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!