-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Involved many-to-many mapping question
PostPosted: Mon Jul 26, 2004 6:48 pm 
Regular
Regular

Joined: Thu Aug 28, 2003 6:30 am
Posts: 58
Hi alls,
I've got table A and table B.
There's many-to-many relation, But also i need position property of class B, which indicates the object B position in object A collection. The concrete object B has different positions in different A objects.


So i think the best solution is

table A Many-to-many table table B
A ---> | a_id | position | b_id | <--- B
/\
|
concrete position

Java classes:
Code:
class B {
  private String name;


  /*
   * property from many-to-many table
   */
  private int position;
  .......
}

class A {
  private List myB;
}


Is it possible to implement in Hibernate?
Or maybe you have some other ideas how i can implement it?

Thank's


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 27, 2004 11:32 am 
Beginner
Beginner

Joined: Wed Jun 23, 2004 12:37 pm
Posts: 20
Have a look at http://www.hibernate.org/118.html#A11

It would seem that many-to-many relationships with attributes are mapped using a composite-element combine with a many-to-one. I personally don't find this intuitive.

I'm trying to get a many-to-many working that links back to the same table and also has an attribute on it. The example from the FAQ seems OK but doesn't result in the 2 foreign keys forming a primary key within the link table.

I'd be interested to see your mappings however you end up getting this working.

Hope this helps,


Nick


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 28, 2004 7:19 am 
Regular
Regular

Joined: Thu Aug 28, 2003 6:30 am
Posts: 58
Nick Redshaw wrote:
Have a look at http://www.hibernate.org/118.html#A11

I'd be interested to see your mappings however you end up getting this working.

Hope this helps,


Nick


Thank's
it helped me.

The table structure is org_id(foreign key to organization), category_id(foreign key to category), position(category position in organization)
Here's my mapping config
Code:
<bag
   name="categories"
   table="category_to_org"
   lazy="true"
   inverse="false"
   cascade="none"
   order-by="position"
   >
   <jcs-cache  usage="read-write" />
   <key column="org_id"/>

    <composite-element
         class="PositionedFeedbackCategory">
        <many-to-one
            name="category"
            class="FeedbackCategory"
            cascade="none"
            outer-join="true"
            update="true"
            insert="true"
            column="category_id"
        />

        <property
            name="position"
            type="int"
            update="true"
            insert="true"
            column="position"
            not-null="false"
            unique="false"
        />

              </composite-element>

        </bag>


And here's composite element class
Code:
public class PositionedFeedbackCategory {
  private int position;
  private IFeedbackCategory category;

  public PositionedFeedbackCategory() {
  }

  /**
   * @hibernate.many-to-one column="category_id"
   * class="FeedbackCategory"
   * outer-join="true"
   */
  public IFeedbackCategory getCategory() {
    return category;
  }

  public void setCategory(final IFeedbackCategory category) {
    this.category = category;
  }

  /**
   * @hibernate.property column="position"
   * not-null="false"
   * unique="false"
   */
  public int getPosition() {
    return position;
  }

  public void setPosition(final int position) {
    this.position = position;
  }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 29, 2004 12:44 pm 
Beginner
Beginner

Joined: Wed Jun 23, 2004 12:37 pm
Posts: 20
we tried it this way but ran into problems when we wanted to query the link table - it seemed that we couldn't get HQL to return objects that were of the type of our link entity.

After looking at the forums I saw a couple of places were the Hib' team advised making link entities into 1st class one by giving them their own <class> in the mapping files. This has made things easier for use on the HQL side.

Good luck with you project.


Top
 Profile  
 
 Post subject: many-to-many w/ details in relation table
PostPosted: Mon Aug 23, 2004 12:21 pm 
Newbie

Joined: Mon Aug 23, 2004 11:06 am
Posts: 1
Location: Bartlett, IL
Hello all,

I'm also running into this many-to-many with relation details in the relation table. In my scenario, I have:

A<---id_A|id_B|A_B_rel_type--->B<---id_B|id_C|B_C_rel_type--->C

Code:
<set name="aBRelationships" table="A_B_REL">
   <key column="ID_A"/>
   <composite-element class="com.blablabla.ABRel">
      <many-to-one name="b" class="com.blablabla.B">
          <column name="ID_B"/>
      </many-to-one>
      <property name="abRelType" column="A_B_rel_type"/>
   </composite-element>
</set>


Because I need to capture the rel_type data, I've had to use the composite mapping w/ many-to-one (see above), creating a Relation object for both A--->B and B--->C. However, if I need to get object A, Hibernate runs 1 + (1 * B objects mapped to A) queries, since stating "outer-join" doesn't seem to work with this methodology (as far as I can tell).

Is it possible to get the full "A" object in one query through straight configuration mapping, without resorting to straight SQL or even HQL? Any help would be appreciated...

Thanks!
Jon Aharrah
jon_aharrah@yahoo.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 3:58 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
This is exactly the problem we have (as of yesterday). Jon, did you make any further progress optimizing your mappings to support more efficient HQL querying?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.