博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate中持久化上下文的flush操作之一AUTO
阅读量:4178 次
发布时间:2019-05-26

本文共 1217 字,大约阅读时间需要 4 分钟。

Hibernate中持久化上下文的flush操作模式默认即为AUTO。在这种模式下,flush操作往往在如下场景下自动执行:

  • 遇到持久化上下文的事务提交,那么在事务提交之前执行flush操作
  • 在即将执行的HQL查询中涉及到了被缓冲的Entity对象
  • 在即将执行的native SQL查询中涉及到了被缓冲的Entity对象

下面以JPA的EntityManager为例介绍flushMode="AUTO"的各个场景。

1.持久化上下文提交事务

entityManager = entityManagerFactory().createEntityManager();txn = entityManager.getTransaction();txn.begin();Person person = new Person( "John Doe" );entityManager.persist( person );//flush executedtxn.commit();

2.即将执行的HQL查询中涉及到了被缓冲的Entity对象

Person person = new Person( "John Doe" );entityManager.persist( person );entityManager.createQuery( "select p from Advertisement p" ).getResultList();//flush executedentityManager.createQuery( "select p from Person p" ).getResultList();

3.即将执行的native SQL查询中涉及到了被缓冲的Entity对象

在这种情况下,JPA的EntityManager将会无条件地执行flush操作,如下所示:

Person person = new Person( "John Doe" );entityManager.persist( person );entityManager.createNativeQuery("select count(*) from Person").getSingleResult();

但是,Hibernate的Session必须在如下情况下才执行flush操作:

Person person = new Person( "John Doe" );entityManager.persist( person );Session session = entityManager.unwrap(Session.class);session.createSQLQuery( "select count(*) from Person").addSynchronizedEntityClass(Person.class).uniqueResult();

转载地址:http://vilai.baihongyu.com/

你可能感兴趣的文章
windows下解决端口占用的情况
查看>>
SpringBoot | 以maven的方式启动项目
查看>>
SpringBoot | 如何配置静态资源的地址与访问路径
查看>>
SpringBoot | 加入shiro之后如何优雅的访问默认目录static下的静态资源
查看>>
DateTimeFormatter时间工具类
查看>>
SpringBoot | 实现切面
查看>>
调用bat文件工具类
查看>>
SpringBoot | 配置fastjson
查看>>
IDEA RESTful Client JSON数据请求
查看>>
DateUtil时间工具类
查看>>
OkHttp3Util工具类
查看>>
获取完整的请求URL
查看>>
Maven常用命令
查看>>
SpringBoot | 运行报错,无法加载oracle连接驱动
查看>>
为什么阿里巴巴禁止在 foreach 循环里进行元素的 remove/add 操作
查看>>
AWS EC2如何从普通用户切换为root用户
查看>>
aws设置root用户通过密码进行登陆
查看>>
click方法不生效的
查看>>
mysql排行榜并列与不并列
查看>>
SpringBoot | Mybatis申明为Mapper文件
查看>>