Java – read only association with JPA onetomany mapping

I have a trading entity associated with another entity, so the associated entity should not be updated in one case

For example Case * – > User

Cases are owned by users. Conversely, users can have many related cases

Use onetomany and joincolumn JPA annotation to map associations

I also try to mark the transactional annotation of the user entity as readonly and make the get method transient However, if the user status changes, this does not seem to stop the update

Please help me find a way to declare a "read only" association to the user

Solution

Yiu can add updatable = false on @ joincolumn annotation

In addition, you should not add setter methods for users in the case entity, nor should you add setter methods for casesets in the user entity Getter getcaseset in userentity should also return a collection that cannot be modified:

public Set<Case> getCaseSet() {
    return Collections.unmodifiableSet(caseSet);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>