• session get和load方法对比


    get测试代码如下:

    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
            SessionFactory sessionFactory=(SessionFactory) ac.getBean("sessionFactory");
            Session session=sessionFactory.openSession();
        
            User user1=(User) session.get(User.class, 3);
            System.out.println("-----------");
            System.out.println(user1);
    
        }
    
    }

    输出结果是:

    Hibernate:
    select
    user0_.id as id0_0_,
    user0_.name as name0_0_
    from
    mydb.user user0_
    where
    user0_.id=?
    -----------
    User [id=3, name=sara3]

    load测试代码

    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
            SessionFactory sessionFactory=(SessionFactory) ac.getBean("sessionFactory");
            Session session=sessionFactory.openSession();
    
            User user2=(User) session.load(User.class,2);
            System.out.println("********");
            System.out.println(user2);
            session.close();
    
        }
    
    }

    输出结果:

    ********
    Hibernate:
    select
    user0_.id as id0_0_,
    user0_.name as name0_0_
    from
    mydb.user user0_
    where
    user0_.id=?
    User [id=2, name=sara2]

    可以看出get方法直接发出sql语句,从数据库加载,没有延迟。load方法只有真正用到对象的内容时才会发出sql语句

    接下来再用两个测试用例

    get测试用例

    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
            SessionFactory sessionFactory=(SessionFactory) ac.getBean("sessionFactory");
            Session session=sessionFactory.openSession();
        
            User user1=(User) session.get(User.class, 3);
            System.out.println("-----------");
            System.out.println(user1.getClass());
    
            session.close();
    
        }
    
    }

    输出结果是:

    Hibernate:
    select
    user0_.id as id0_0_,
    user0_.name as name0_0_
    from
    mydb.user user0_
    where
    user0_.id=?
    -----------
    class com.db.entity.User

    load测试代码:

    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
            SessionFactory sessionFactory=(SessionFactory) ac.getBean("sessionFactory");
            Session session=sessionFactory.openSession();
        
            User user2=(User) session.load(User.class,2);
            System.out.println("********");
            System.out.println(user2.getClass());
            session.close();
        }
    }

    输出结果是:

    ********
    class com.db.entity.User_$$_javassist_0

    结论:

      get方法会在调用之后立即向数据库发出sql语句(不考虑缓存的情况下),返回持久化对象;而load方法会在调用后返回一个代理对象,该代理对象只保存了实体对象的id,直到使用对象的非主键属性时才会发出sql语句

  • 相关阅读:
    Java中的final关键字解析
    twaver html5 如何设置节点不可拖动
    Redis Cluster 强制kill某一个节点和shutdown某一个节点后修复过程
    Redis Cluster 4.0.9 集群安装搭建
    Redis Cluster 获取主从关系
    Linux 只复制目录,不复制目录下数据文件
    安装redis cluster时:undefined method `invoke_with_build_args' for nil:NilClass
    redis : Can't save in background: fork: Cannot allocate memory
    MySQL 5.6 GTID 原理以及使用
    pt-query-digest使用详解
  • 原文地址:https://www.cnblogs.com/SaraMoring/p/5655456.html
Copyright © 2020-2023  润新知