While using OpenJPA, if I mark a class with annotation @Entity, is there a way to specify(through annotation) that always keep this entity in sync with the underlying db.
The objective is to always access the latest modified data.I understand that outside the entity class, we can do an EntityManager.refresh and that would refresh the object to load the latest data from the db. But is there a way to specify it while creating the Entity itself. So that a future developer can just look at the entity class and know that it will always have latest data. He does not need to scan the whole code base to see where EntityManager.refresh has been called.
I think the problem you are dealing with is concurrent modifications of the same entity. JPA is not designed to deal with that. More appropriate forms of handling concurrent access in JPA applications is locking.
You also don’t mention transaction isolation. The behavior you speak about heavily depends on the accessibility of data, that was modified after the transaction started. That is not possible in all transaction isolation levels.
You should compare your requirements more to something like a system clock. You always want to access the latest date when invoking a method
now. So you should have a service that can return you that object. And you should also write updates for that object through that service. So the service has full control over the object that is currently most up to date.