简体   繁体   English

Hibernate 禁用脏检查

[英]Hibernate disable dirty checking

I would like to globally disable dirty checking in my application (spring data, hibernate).我想在我的应用程序(spring 数据、hibernate)中全局禁用脏检查。 My goal is that JPA generates update/insert queries only when I explicitly call myRepo.save() or myRepo.myUpdateQuery() .我的目标是 JPA 仅在我明确调用myRepo.save()myRepo.myUpdateQuery()时生成更新/插入查询。

All my entities have final fields and unmodifyiable collections, so I really do not need dirty checking.我所有的实体都有最终字段和不可修改的 collections,所以我真的不需要脏检查。 I always handle updates explicitly.我总是明确地处理更新。

Still, I get massive performance problems because JPA generates redundant update queries every time a field gets lazy-loaded.尽管如此,我还是遇到了巨大的性能问题,因为每次延迟加载字段时 JPA 都会生成冗余更新查询。

Solutions I have seen so far (no success):到目前为止我看到的解决方案(没有成功):

  • Use FlushMode=MANUAL.使用 FlushMode=MANUAL。 This is an overkill since I will have to make sure flush() gets called manually.这是一个大材小用,因为我必须确保手动调用flush() Huge refactoring I cannot afford.我负担不起巨大的重构。

  • Use readonly transactions.使用只读事务。 This would also block my explicit save/modifying query calls.这也会阻止我的显式保存/修改查询调用。 I want those to keep working.我希望他们继续工作。

  • Detach the entities.分离实体。 This will has extra side effects such as not being able to perform further lazy-loads, which is a nogo.这将产生额外的副作用,例如无法执行进一步的延迟加载,这是不行的。 Also, it will require huge refactoring.此外,它将需要大量的重构。 For the most part, I do not even have access to EntityManager since I am using spring data repositories.在大多数情况下,我什至无法访问 EntityManager,因为我使用的是 spring 数据存储库。

  • Use Stateless Sessions.使用无状态会话。 Not an option since things like Lazy Loading will also get disabled.不是一个选项,因为延迟加载之类的东西也会被禁用。

Use FlushMode=MANUAL.使用 FlushMode=MANUAL。 This is an overkill since I will have to make sure flush() gets called manually.这是一个大材小用,因为我必须确保手动调用 flush()。 Huge refactoring I cannot afford.我负担不起巨大的重构。

If you don't want to do that, there is not much you can do if you also need lazy loading.如果您不想这样做,如果您还需要延迟加载,那么您无能为力。 Maybe you can load entities as read only (see org.hibernate.Session#setReadOnly and org.hibernate.Session#setDefaultReadOnly ) and upgrade them to become mutable, but that will again require calling explicit methods just like calling flush explicitly.也许您可以将实体加载为只读(请参阅org.hibernate.Session#setReadOnlyorg.hibernate.Session#setDefaultReadOnly )并将它们升级为可变的,但这将再次需要调用显式方法,就像显式调用flush一样。

I can tell you from past projects though, that it is well worth the refactoring to ensure no lazy loading happens outside of the service layer.不过,我可以从过去的项目中告诉您,重构以确保服务层之外不会发生延迟加载是非常值得的。

Maybe a DTO approach like Blaze-Persistence Entity-Views is what you are looking for?也许您正在寻找像Blaze-Persistence Entity-Views这样的 DTO 方法? It's read only by default, but you can convert your read only data to updatable types on demand to model your write concerns.默认情况下它是只读的,但您可以根据需要将只读数据转换为可更新类型,以解决您的写入问题 model。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM