Clear方法
Clear方法会完全清除Hibernate中的缓存。下面是两个例子:
Session session = HibernateUitl.getSessionFactory().getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher) session.get(Teacher.class, 3);
System.out.println(t.getName());
Teacher t2 = (Teacher) session.get(Teacher.class, 3);
System.out.println(t2.getName());
session.getTransaction().commit();
session.close();
上面虽然有两次查询,但由于第一次查询之后,HIbernate已经将id为3的Teacher缓存起来了,因此最终只会产生一条SQL语句。
再看看下面这个例子:
Session session = HibernateUitl.getSessionFactory().getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher) session.get(Teacher.class, 3);
System.out.println(t.getName());
session.clear();
Teacher t2 = (Teacher) session.get(Teacher.class, 3);
System.out.println(t2.getName());
session.getTransaction().commit();
session.close();
在第二次查询之前,我们先调用了session.clear()
方法,最终产生了两天SQL查询语句。
再看看下面这个例子:
Session session = HibernateUitl.getSessionFactory().getCurrentSession();
session.beginTransaction();
Employee emp1 = (Employee)session.get(Employee.class, 1);
Employee emp2 = (Employee)session.get(Employee.class, 2);
//emp1 and emp2 are in persistent state.
emp1.setEmpName("NEW SREE");
emp2.setEmpName("NEW KALYAN");
session.clear(); //emp1 and emp2 are in detached state.
session.getTransaction().commit();
session.close();
运行后发现emp1和emp2并没有被更新,这也是由于我们在commit这个事务前调用了session.clear()
方法。
Evict方法
Evict方法和Clear方法类似,但是它作用于单个Entity。
看下面这个例子:
Session session = HibernateUitl.getSessionFactory().getCurrentSession();
session.beginTransaction();
Employee emp1 = (Employee)session.get(Employee.class, 1);
Employee emp2 = (Employee)session.get(Employee.class, 2);
//emp1 and emp2 are in persistent state.
emp1.setEmpName("NEW SREE");
emp2.setEmpName("NEW KALYAN");
session.evict(emp1); //emp1 is in detached state and emp2 is in persistent state.
session.getTransaction().commit();
session.close();
运行后发现emp2被成功更新了,而emp1并未改变。
Flush方法
Flush方法可以强制将持久态的对象从内存同步到数据库,但并不会清除缓存。
看下面的例子:
Session session = HibernateUitl.getSessionFactory().getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher) session.get(Teacher.class, 3);
t.setName("name1");
t.setName("name2");
session.getTransaction().commit();
session.close();
上面的方法调用了两次setName()
方法,但最终只会产生一条SQL语句。
再看下面的例子:
Session session = HibernateUitl.getSessionFactory().getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher) session.get(Teacher.class, 3);
t.setName("name1");
session.flush();
t.setName("name2");
session.getTransaction().commit();
session.close();
在第二次调用setName()
方法前,我们调用了session.flush()
方法,因此会产生两天SQL语句,因为flush方法会强行同步内存和数据库中持久态的对象。
注意!flush方法并不意味着数据已经持久化到数据库中了,在没有提交事务前,所有的数据都并没有真正被持久化。
Transaction commit时,实际上隐式地调用了flush方法来同步内存和数据库。
通常,为了控制Hibernate一级缓存的大小,我们会手动调用flush
方法和clear
方法。下面是一个例子:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
if ( i % 20 == 0 ) { //20, same as the JDBC batch size
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
网友评论