简体   繁体   中英

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. I execute only select statements and recalculate value of some field for my needs, but do not want it to be updated automatically. 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); 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?

My machine (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)

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. Rolling back the transaction is pretty much the same as clearing or closing the EntityManager. Detached entities will only have changes persisted if you merge them back into the EntityManager.

You also can do reads outside a transaction if not using JTA. If the EM is not associated to a transaction, changes cannot get flushed. In this way, you can use an EM that is not associated to a transaction and just discard the EntityManager when done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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