-->
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.  [ 2 posts ] 
Author Message
 Post subject: Bidirectional Many-to-many mapping pitfalls
PostPosted: Sat Apr 23, 2011 4:11 pm 
Newbie

Joined: Sat Apr 23, 2011 3:50 pm
Posts: 3
Greetings. Forgive me if this has been asked before - I did try the local search feature and google, but neither provided an answer, so I'll just dive in.

I am building an Orcale XE database to hold information about lego parts (we won't discuss how big a geek that makes me) and there is a many to many relationship between the part (such as block 1x1 or plate 4x8) and the set it belongs to (such as X-Wing Starfighter). A set, obviously, has many parts, and each part can belong to more than one set.

The tables involved are PARTS, SETS, and a cross table called SET_PARTS.

I have the hibernate mappings declared for PARTS and SETS, and I already have the many to many definition in the Sets class declared (getParts()).

What I would like to do is include the reverse mapping in the Parts class (getSets()). My question is: Will this cause issues in the application, or is Hibernate smart enough not to load all the parts for all the sets that I am loading by calling getSets (i.e. I could see this crashing the application with an infinite loading sequence, so I am wondering if there is something built into Hibernate that prevents it).

My current hbm files are below - they're fairly short. The mapping from parts to sets is the one I am concerned about adding.

Any assistance would be great.

Thanks.

PARTS
Code:
<hibernate-mapping package="com.legolibrary.hibernate">
  <class name="Parts" table="PARTS">
     <id name="id" type="long" column="PART_ID" >
      <generator class="sequence" >
         <param name="sequence">PART_ID_SEQ</param>
      </generator>
   </id>
        <property name="legoid" column="LEGO_OFFICIAL_PART_CODE" type="string"/>
   <property name="description" column="DESCRIPTION" type="string"/>
   <many-to-one name="color"
         column="COLOR_REF_ID"
         class="com.legolibrary.hibernate.ReferenceData"
         not-null="true"
         foreign-key="PART_COLOR_FK"
         lazy="false" />
   <set name="sets" table="SET_PARTS" cascade="all">
      <key column="SET_PART_ID" />
      <many-to-many column="SET_ID"  class="com.legolibrary.hibernate.Sets" />
   </set>
  </class>
</hibernate-mapping>



SETS
Code:
<hibernate-mapping package="com.legolibrary.hibernate">
  <class name="Sets" table="SETS">
     <id name="id" type="long" column="SET_ID" >
      <generator class="sequence" >
         <param name="sequence">SET_ID_SEQ</param>
      </generator>
   </id>
        <property name="legoid" column="LEGO_OFFICIAL_SET_NUM" type="string"/>
   <property name="name" column="NAME" type="string"/>
   <set name="parts" table="SET_PARTS" cascade="all">
      <key column="SET_PART_ID" />
      <many-to-many column="PART_ID"  class="com.legolibrary.hibernate.Parts" />
   </set>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Bidirectional Many-to-many mapping pitfalls
PostPosted: Tue Apr 26, 2011 3:08 am 
Newbie

Joined: Mon Apr 25, 2011 7:23 am
Posts: 1
What ever the crash problem you are getting is very obvious problem with bi-directional many to many mapping.

One thing you could do is use lazy loading of associations in both entities. And try to use Hibernate.initialize(...) method to lazy load required entities.

Other approach would be to remove bi-directional association... meaning remove many to many mapping at one side and use HQL to query the required entities.

Best of Luck,
Srinivas

_________________
Regards,
Srinivas Bodduluri


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.