简体   繁体   English

当我更改Embedded对象的字段时,EntityManager会自动对数据库执行UPDATE,但不要调用merge或flush

[英]EntityManager performs UPDATE to database automatically when I change a field of Embedded object, but do not call merge or flush

I have some bean method that causes undesired updates to database. 我有一些bean方法导致不希望的数据库更新。 I execute only select statements and recalculate value of some field for my needs, but do not want it to be updated automatically. 我只执行select语句并根据我的需要重新计算某些字段的值,但不希望它自动更新。 How can I control this process without intervention to the default application settings? 如何在不干预默认应用程序设置的情况下控制此过程?

Log file says about some mystical background flush process: 日志文件说明了一些神秘的背景刷新过程:

begin unit of work flush    
Execute query UpdateObjectQuery ...

I can avoid this updates making em.setFlushMode(FlushModeType.COMMIT); 我可以避免这个更新制作em.setFlushMode(FlushModeType.COMMIT); in my method. 在我的方法。 And that is really strange on my machine this works Ok - no updates during method executions and after. 这在我的机器上真的很奇怪这个工作正常 - 在方法执行期间和之后没有更新。 But on client machine I also need revert transaction - only in this case I do not see any update statements in my log. 但是在客户端机器上我还需要还原事务 - 只有在这种情况下我才会在日志中看到任何更新语句。 But is this cure method correct? 但这种治疗方法是否正确? Will other threads perform autocommits when I change FlushMode for EM in my bean method? 当我在bean方法中更改FlushMode for EM时,其他线程会执行自动提交吗?

My machine (GlassFish 2.0, Ubuntu 12.10, eclipcelink 3.2, jdk 1.7.0_15) 我的机器(GlassFish 2.0,Ubuntu 12.10,eclipcelink 3.2,jdk 1.7.0_15)

Client machine (GlassFish 2.0, Win 7 x86_64, eclipcelink 3.2, jdk 1.7.0_15) 客户机(GlassFish 2.0,Win 7 x86_64,eclipcelink 3.2,jdk 1.7.0_15)

If you want the objects but don't want changes to be persisted, you need to detach it from the EntityManager using em.detach(entity) if using JPA 2.0 or em.clear() if you want everything removed. 如果您想要对象但不希望保留更改,则需要使用em.detach(实体)将其从EntityManager中分离(如果使用JPA 2.0或em.clear(),如果您希望删除所有内容)。 Rolling back the transaction is pretty much the same as clearing or closing the EntityManager. 回滚事务与清除或关闭EntityManager几乎相同。 Detached entities will only have changes persisted if you merge them back into the EntityManager. 如果将它们合并回EntityManager,则分离的实体将仅保留更改。

You also can do reads outside a transaction if not using JTA. 如果不使用JTA,您也可以在事务外进行读取。 If the EM is not associated to a transaction, changes cannot get flushed. 如果EM未与事务关联,则无法刷新更改。 In this way, you can use an EM that is not associated to a transaction and just discard the EntityManager when done. 通过这种方式,您可以使用与事务无关的EM,并在完成后丢弃EntityManager。

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

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