• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Defensive copying with JPA

 
Jason Nerothin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about the appropriate way to use the defensive copying pattern in JPA.

Basically, I'm trying to implement a static valueOf() method on value classes related bidirectionally to each other by @ManyToOne or @ManyToMany annotations such that each side of a relationship is appropriately maintaining references back and forth.

For example:


The problem is recursive: Each valueOf() method needs to call the valueOf() on the other side of the relationship since I want a faithful copy of each Object in my heirarchy.

Do I implement my classes in such a way that only the owning (or owned) side of the relationship actually calls the valueOf() method? Since I can't know a priori whether my Object is going to be attached or detached from the container it seems like I'm heading for trouble since I'm required by convention to keep both sides of any JPA relationship updated.

What am I missing?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic