• [Hibernate]


    Hibernate中使用session的load(...)和get(...)区别:

    1、load()方法是在返回的类被后期调用时,才会去提交SQL查询数据库。

    2、get()方法是在调用get()时就即时提交SQL查询数据库。

    3、load()方法如果查找不到数据,会抛出ObjectNotFoundException的异常。

    4、get()方法如果查找不到数据,不会抛异常,但会返回一个null对象。

    5、load()和get(),都会先到session的缓存中查找,如果找到,直接缓存返回对象,如果找不到才去数据库查找对象。

    6、均可以使用session.evict()方法清除缓存。

    示例:

    package com.my.init;
    
    import org.hibernate.ObjectNotFoundException;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    
    import com.my.bean.User;
    import com.my.dao.util.HibernateUtil;
    
    public class Test {
    
        public static void main(String[] args) {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
    
            try {
                // ---------------------------------------------------------
                // Load class
                // ---------------------------------------------------------
                User user1 = (User) session.load(User.class, (long) 1);
                System.out.println("Load class");
                System.out.println("User name:" + user1.getUserName());
    
                // ---------------------------------------------------------
                // Get class
                // ---------------------------------------------------------
                User user2 = (User) session.get(User.class, (long) 2);
                System.out.println("Get class");
                System.out.println("User name:" + user2.getUserName());
    
                // ---------------------------------------------------------
                // Load cache class
                // ---------------------------------------------------------
                User user3 = (User) session.load(User.class, (long) 1);
                System.out.println("Load cache class");
                System.out.println("User name:" + user3.getUserName());
    
                // ---------------------------------------------------------
                // Get cache class
                // ---------------------------------------------------------
                User user4 = (User) session.get(User.class, (long) 2);
                System.out.println("Get cache class");
                System.out.println("User name:" + user4.getUserName());
    
                try {
                    // ---------------------------------------------------------
                    // Load class throw ObjectNotFoundException
                    // ---------------------------------------------------------
                    User user5 = (User) session.load(User.class, (long) 3);
                    System.out.println("User name:" + user5.getUserName());
                } catch (ObjectNotFoundException e) {
                    System.out.println("Load class throw ObjectNotFoundException");
                }
    
                // ---------------------------------------------------------
                // Get class return null
                // ---------------------------------------------------------
                User user6 = (User) session.get(User.class, (long) 3);
                if (user6 == null) {
                    System.out.println("Get class return null");
                }
    
                // ---------------------------------------------------------
                // Remove User cache
                // ---------------------------------------------------------
                session.evict(user1);
                User user7 = (User) session.load(User.class, (long) 1);
                System.out.println("Load class");
                System.out.println("User name:" + user7.getUserName());
    
                // ---------------------------------------------------------
                // Remove User cache
                // ---------------------------------------------------------
                session.evict(user2);
                User user8 = (User) session.get(User.class, (long) 2);
                System.out.println("Get class");
                System.out.println("User name:" + user8.getUserName());
    
                // ---------------------------------------------------------
                // Remove session all cache
                // ---------------------------------------------------------
                session.clear();
    
                tx.commit();
            } catch (Exception e) {
                tx.rollback();
                e.printStackTrace();
            }
    
            session.close();
        }
    
    }

    上面代码的执行结果:

  • 相关阅读:
    python __builtins__ set类 (60)
    python __builtins__ reversed类 (58)
    python __builtins__ range类 (56)
    python __builtins__ property类 (55)
    python __builtins__ memoryview类 (46)
    python __builtins__ map类 (44)
    python __builtins__ list类 (42)
    python __builtins__ license类 (41)
    (转)面试算法总结
    (Mark)JS中的上下文
  • 原文地址:https://www.cnblogs.com/HD/p/3958111.html
Copyright © 2020-2023  润新知