I have an method in DAO like below,
public void updateEntity1(Entity2) {
Entity1 = entitymanager.find(....);
Entity1.setAttr(Entity2.getAttr());
.........
entitymanager.merge(Entity1);
em.flush();
}
I want update entity1 with the value entity2 contains, at the end of the method, I find entity1's attr updated successfully, but the in database it didn't changed? Is there any thing wrong with this method?
Try enclosing your code by in a transaction as below:
entityManager.getTransaction().begin();
....//your code
entityManager.getTransaction().commit();
If your transaction is already available (otherwise try @Madhusudana Reddy Sunnapu solution) try with:
public void updateEntity1(entity2) {
Entity1 entity1 = entitymanager.find(...);
Entity1.setAttr(entity2.getAttr());
...
entity2.detach();
entitymanager.merge(entity1);
em.flush();
}
thanks for help. I find out the interesting reason, it is about the search cache:
the Entity1 was in cache due to a former search, eg it's attr=1;
but in another transaction, it's attr was setted to 2, so the cache data is dirty data,
above I just want to set Entity1 attr back to 1,but the cache data is still 1, so JPA thought I didn't make any change, so em.merge() do nothing.
that's quite impressive!
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.