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(); } }
上面代码的执行结果: