-->
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.  [ 3 posts ] 
Author Message
 Post subject: Best practise for many-to-many relation
PostPosted: Sun Jan 13, 2008 5:26 pm 
Newbie

Joined: Sun Dec 30, 2007 12:51 pm
Posts: 7
I got following tables :

Items
itemId

Materials
materialId

ItemMaterials
itemId
materialId

So item to material is many-to-many relation.

I am going to have more than 5000-6000 items in database and about 20-30 materials.

According to my nhibernate knowledge i have to use following classes and mappings :

class Item
{
int id;
IList<Materials>
}

class Material
{
int id;
IList<Items>
}

and following mappings :

<class name="Item" table="`Items`" >
<bag name="Materials" table="`Materials`" lazy="true" inverse="true">
<key column="`itemId`"/>
<many-to-many class="Material" column="`materialId`"/>
</bag>
</class>

<class name="Material" table="`Materials`" >
<bag name="Items" table="`Items`" lazy="true">
<key column="`materialId`"/>
<many-to-many class="Item" column="`itemId`"/>
</bag>
</class>

To be honest i do not need the reference from Material to Items but i believe this is necessary for this to work.

Now i have one problem.

To add relation between item nad Material i believe i have to do something like that

Item it1 = new Item();
Material mat1 = new Material();

it1.materials.add(mat1);
mat1.items.add(it1);

My problem is that if this mat1 has 5000 more items connected to it, would it fetch all these items from database during the add command when i add this new it1 item ? This would be very very inefficient.

Thank you in anticipation for reply.
Regards,
Piotr


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 13, 2008 5:57 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Piotr,

-The table "ItemMaterials" is mentioned nowhere in your mapping.
-if you do not declare the relationship as bidirectional, (and you are saying that you don't need it) you don't need to write both ends of the relationship, In other words, you don't need to retrieve your Items given a Material, nor set the parent Item on the new Material.
-It is a good practice to add a method "addMaterial" to the item, which would add the material to the internal list of the item, and (if you want to keep it bidirectional) add the parent Item on the new material's list with an "addItem".
I am not sure how Hibernate manages that internally, but the list wont get initialized until you don't call the getter

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 13, 2008 6:47 pm 
Newbie

Joined: Sun Dec 30, 2007 12:51 pm
Posts: 7
Thank you gonzao_diaz,
your reply was very helpful.


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