clear是Hibernate一种强制清除所有session缓存的方法。 无论是load还是get,都会查找缓存(一级缓存),如果没有才会去数 据库查找,调用clear()方法强制清除session缓存
public void clear() {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Temp temp2 = session.get(Temp.class, "a1");
System.out.println("get" + temp2);
//提交
transaction.commit();
// session.clear(); 用于清空一级缓存中缓存的所有对象
//关闭
session.close();