4 Replies Latest reply on Mar 20, 2007 4:46 PM by joe.marques

    ManyToMany enhancement?

    Newbie

      Given 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

        • 1. Re: ManyToMany enhancement?
          Newbie

          Even cooler would be to support an extended JPQL syntax that had the best of both worlds. So, something like:

          SELECT someA FROM A someA, IN (someA.Bs) someB WHERE someB.stuff=<value>
          


          Thus, by traversing a ManyToMany you somehow get auto-magical access to the additional qualifiers in the linking table through the relation.

          • 2. Re: ManyToMany enhancement?
            Tobias Reese Newbie

            I am looking for exactly this feature too. Can't find anything even getting close to a acceptable solution...

            • 3. Re: ManyToMany enhancement?
              yakup güne? Newbie

              how was Object_B merge operation

              • 4. Re: ManyToMany enhancement?
                Newbie

                 

                how was Object_B merge operation


                I don't really understand the question...but I'll attempt to answer what I think your asking anyway. ;)

                My best guess tells me your question targets my latter post:

                SELECT someA FROM A someA, IN (someA.Bs) someB WHERE someB.stuff=<value>


                ...which pertains to how Hibernate would do the merge of the additional qualifying data (stuff) into some object (someB) for the traversal (someB.stuff).

                Well, as it stands today, Hibernate3 returns proxy objects. So I was thinking that it wouldn't be a stretch of the imagination to use that concept here in this case.

                One might be able to generate proxies that are the extension of the domain object (e.g., class B), but which also include attributes that represent the additional qualifying information (Stuff). So the proxy might have an effective definition that looked like:

                class B_proxy {
                 List<A> As; // because proxy extends class B
                 String stuff; // assuming stuff was a string qualifier
                 ...
                }


                If this "extended" proxy were returned by the query, then I think it would be possible to get the "auto-magical access to the additional qualifiers".