本文共 683 字,大约阅读时间需要 2 分钟。
对于Hibernate中的两种持久化上下文,JPA的EntityManager和Hibernate的Session,两者都提供了flush()方法。任何时候在应用中调用该方法,则触发持久化上下文与数据库的同步。
除此之外,还可以为Hibernate的Session设置flush模式为MANUAL(JPA的EntityManager不支持),即Hibernate的Session只支持手工执行flush操作,示例如下:
Person person = new Person("John Doe");entityManager.persist(person);Session session = entityManager.unwrap( Session.class);session.setFlushMode( FlushMode.MANUAL);entityManager .createQuery("select count(id) from Person") .getSingleResult();session .createSQLQuery("select count(*) from Person") .uniqueResult();//flush executedsession.flush();在上述示例中,执行native SQL查询并不能触发Hibernate的Session执行flush操作,只有调用Hibernate的Session的flush()方法时才触发Hibernate的Session执行flush操作。
转载地址:http://ailai.baihongyu.com/