Reputation: 7
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?
Upvotes: 0
Views: 81
Reputation: 7
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, e.g. 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!
Upvotes: 0
Reputation: 5828
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();
}
Upvotes: 0
Reputation: 8247
Try enclosing your code by in a transaction as below:
entityManager.getTransaction().begin();
....//your code
entityManager.getTransaction().commit();
Upvotes: 1