ManyToMany enhancement?
joe.marques Jan 19, 2007 3:15 AMGiven this object model:
Object_A LinkT Object_B Aid --- Aid Bid --- Bid Stuff
I would like to use the following ManyToMany style on classes A & B:
@Entity @Table(name = "Object_A") class A { @ManyToMany @JoinTable(name="LinkT", joinColumns={@JoinColumn(name="Aid")}, inverseJoinColumns={@JoinColumn(name="BiD")}) List<B> Bs = new ArrayList<B>(); } @Entity @Table(name = "Object_B") class B { @ManyToMany(mappedBy="Bs") List<A> As = new ArrayList<A>(); }
But I also want to be able to access the linking table directly, because it has extra metadata called "stuff" that I insert directly into it with a different Entity mapped directly on top of it:
@Entity @Table(name = "LinkT") class Link { // Aid, Bid, and Stuff }
Thus, I can do one of two things right now:
1) Use the @ManyToMany annotations to construct the relationships. This allows me to navigate via getAs() and getBs() to the other side of the relationship and back again. However, doing it this way prevents me from inserting the additional "stuff" qualifier, which can only be calculated at insertion-time.
2) I can use a slightly more obtuse procedure to insert data into the linking table via the Link entity. Here, I've ensured that the data in all three tables is correct, and that the relational part is identical to what is persisted in the above scenario. This method affords me the additional opportunity to insert this qualifying "stuff" into the linking table. Unfortunately, when I go to access the relationships - getAs() and getBs() - they always return empty sets.
Now, I went into all of this presuming that this was most likely a limitation of the current JBoss ejb3 implementation (inserting data through a direct map on top of the linking table, and selecting from the @ManyToMany context), but it would sure be nice if this was a JBoss extension.
After the data is inserted into the linking table with the additional "stuff" attached to it, then you could choose the complexity of your JPQL according to the needs in your calling context:
A simple condition:
SELECT someA FROM A someA, IN (someA.Bs) someB WHERE someB.property=<value>
A complex condition, requiring the use of "stuff":
SELECT someA FROM A someA, LinkT link WHERE someA.Aid = link.Aid AND link.stuff=<value>
Thoughts on this?
-joseph