I've got situations where I want to implement an ad hoc 1-many relationship
from one CMP entity to a number of other CMP entities. By ad hoc, I mean
that there is no data in the target entities which would allow me to use a
finder method that returns the right collection of entities. A simple,
obvious approach to implementing that would be for the first entity to have
a CMP field which is a Java2 Collection. The tricky part is what specific
object type to put in the collection. The most obvious ways to do that
would be to directly add the target Entity (using its EJBObject reference)
or to add its Handle. Unfortunately, neither of these work, because neither
EJBObject nor Handle provide the equals() semantics required for use in
Collections. The EJB spec does not require EJBObject or Handle to have
equals implementation that will return true if and only if they refer to
the identical object (I think that's a deficiency of the spec, but I'm
trying to work with the current spec and not counting on that getting fixed
anytime soon). The EJBObject isIdentical method supports the right
semantics, but of course the Collections don't use it.
Primary key objects would also not be sufficient in general, although they
could work if the instances were restricted to being all managed by the
same Home.
The approach I'm thinking of using is to create my own
EntityReferenceAdapter class, which would have one attribute which is an
EJBObject reference, one constructor that takes an EJBObject reference as
input, and an override of the equals method that returns the result of
calling isIdentical on the EJBObject (or returns false if the parameter
passed on the equals call isn't even an EJBObject). Then I'd put instances
of EntityReferenceAdapter into the Java2 Collection.
A different approach I've seen is to have specialized collection classes
designed for EJB support, but I'm leaning toward this adapter approach
because with only a small effort on my part it lets me use any Java2
Collection class.
Have others dealt with this? Does this seem like a good approach? Anyone
have suggestions for a better approach?
Erik Voldal
Email: [EMAIL PROTECTED]
Phone: 507-253-4788
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".