java 更新前休眠选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16303691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:34:40  来源:igfitidea点击:

Hibernate select before update

javahibernateentity-relationship

提问by Damask

In my work i'm using spring with exidirect, hibernate on the server side and extjs on client side. When i post a form, on the server side spring converts it to the entity. Entity has a id field, that supposes update operation. I'm calling service savemethod, but instead one sql updatequery i get many many select queries and just then update. It takes much time. And there is no need for this operation. I was looking similar questions and was trying to use persistmethod. This case i get error: detached entity passed to persist.

在我的工作中,我将 spring 与 exidirect、hibernate 在服务器端和 extjs 在客户端一起使用。当我发布表单时,在服务器端 spring 将其转换为实体。实体有一个 id 字段,假设更新操作。我正在调用服务保存方法,但是我收到了许多选择查询,而不是一个 sql更新查询,然后才更新。这需要很多时间。并且没有必要进行此操作。我正在寻找类似的问题,并试图使用持久方法。在这种情况下,我收到错误:将分离的实体传递给持久化。

I do not have enough experience of hibernate. May be i need to configure related entities (OneToMany, ManyToOne and cascade types). Entities are generated by Spring roo tool.

我没有足够的休眠经验。可能是我需要配置相关实体​​(OneToMany、ManyToOne 和级联类型)。实体由 Spring roo 工具生成。

Any suggestions ? Thank you.

有什么建议 ?谢谢你。

回答by Rafa

This is not a final answer for your question but I hope it can work as a high level guideline.

这不是您问题的最终答案,但我希望它可以作为高级指南。

When you create an Entity with Spring, that Entity is detached and Hibernate performs these SELECT statements because it needs to attach your Entity before persisting it. Additionally, as far as I know any attempt to attach an Entity is going to trigger SELECT statements. Therefore, I strongly believe that there is not any way to save/persist your detached Entities without these SELECT statements (I mightbe wrong here).

当您使用 Spring 创建一个实体时,该实体被分离并且 Hibernate 执行这些 SELECT 语句,因为它需要在持久化之前附加您的实体。此外,据我所知,任何附加实体的尝试都会触发 SELECT 语句。因此,我坚信没有这些 SELECT 语句就没有任何方法可以保存/保留分离的实体(我在这里可能错了)。

If you are concern about performance you can try adding some cache functionality to your application. Moreover, you can also include JDBC solutions only for operations which are suffering with performance.

如果您担心性能,可以尝试向应用程序添加一些缓存功能。此外,您还可以仅针对性能不佳的操作包含 JDBC 解决方案。

回答by Dmitry

Add @SelectBeforeUpdate(false) annotation to the entity.

向实体添加 @SelectBeforeUpdate(false) 注释。