users@glassfish.java.net

JPA design question

From: <glassfish_at_javadesktop.org>
Date: Sat, 10 Jan 2009 11:06:57 PST

I need to have an entity that needs to reference two other entities. For the majority of the use cases, only knowing that the reference to the other entities exists is sufficient; the relationship will not generally be traversed. However, if the referenced entities are removed, then the first entity needs to be updated and the references cleaned up.

So I am wondering what is the best way to model this in JPA. I could have full @OneToOne references in the first entity to the other two entities or I could just store @Id of the other two entities. Either way, however, I need to ensure that when the referenced entities are removed, then these relationships are cleaned up.

In straight POJO, I would probably define a listener interfaces that the first entity would implement and register with the referenced entities, but I don't know if that is viable in JPA. I would consider using a JPA listener and use the @PreDestroy or @PostDestroy but from my reading these cannot (should not) perform other EntityManager persistent actions.

So does anyone have any suggestions?

Brett
[Message sent by forum member 'bbergquist' (bbergquist)]

http://forums.java.net/jive/thread.jspa?messageID=325169