-->
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.  [ 10 posts ] 
Author Message
 Post subject: Many-to-Many relatinships with attributes,.. how to map it?
PostPosted: Tue Oct 17, 2006 4:55 pm 
Newbie

Joined: Fri Oct 06, 2006 1:54 pm
Posts: 5
Location: Costa Rica
I have the following tables:

Person
--------------
id integer
name string

Group
--------------
id integer
name string
description string

Person_Group
------------------
person_id integer
group_id integer
is_leader boolean (short)


This are de mappings
Code:
PERSON MAPPING
<hibernate-mapping>
  <class table="person" name="data.model.Person">
    <id type="integer" name="id">
      <generator class="identity"/>
    </id>
    <property name="name" type="string" column="name"/>
    <set table="person_group" batch-size="5000" lazy="true" inverse="true" name="personGroups">
      <key column="person_id"/>
      <many-to-many column="group_id" class="data.model.Group"/>
    </set>
  </class>
</hibernate-mapping>

GROUP MAPPING

<hibernate-mapping>
  <class table="group" name="data.model.Group">
    <id type="integer" column="id" name="id">
      <generator class="identity"/>
    </id>
    <property name="name" type="string" column="name"/>
    <set table="person_group" batch-size="5000" lazy="true" inverse="true" name="groupPersons">
      <key column="group_id"/>
      <many-to-many column="person_id" class="data.model.Person"/>
    </set>
  </class>
</hibernate-mapping>



Person_Group is not mapped to a class, because I'm using Collections to hold the relationship, but I cant't map the is_leader column. Now I need querys like "the leaders for a group" or "which groups lead a person"

Do I need map person_group table with a class? or it's possible do that queries without extra mapping?

_________________
Carpe Diem!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 12:55 pm 
Newbie

Joined: Fri Oct 06, 2006 1:54 pm
Posts: 5
Location: Costa Rica
Someone tell me that its necesary map the Person_Group table, any idea how to map that table and get the information?

_________________
Carpe Diem!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 1:02 pm 
Newbie

Joined: Thu Nov 10, 2005 6:05 am
Posts: 16
Why not simply add a Person field to the Group class which points to the leader of the group? I think it's really the group's property who leads it.

Code:
<hibernate-mapping>
  <class table="group" name="data.model.Group">
    <id type="integer" column="id" name="id">
      <generator class="identity"/>
    </id>
    <property name="name" type="string" column="name"/>
      <many-to-one name="leader" class="data.model.Person" column="leaderId" unique="true" not-null="true" />
    <set table="person_group" batch-size="5000" lazy="true" inverse="true" name="groupPersons">
      <key column="group_id"/>
      <many-to-many column="person_id" class="data.model.Person"/>
    </set>
  </class>
</hibernate-mapping>


rgrds,
Ivan[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 2:35 pm 
Newbie

Joined: Fri Oct 06, 2006 1:54 pm
Posts: 5
Location: Costa Rica
The thing is that more than one person can be group leader. This because in the reality ( the reason of the application I mean) need in a group leaders in diferent locations, thats why a group can have 1 or more leaders.

The mapping you show is the Group Mapping. How Hibernate knows that

many-to-one name="leader" class="data.model.Person" column="leaderId" unique="true" not-null="true" />

leaderId its from person_group? or I need to change the table structure?

_________________
Carpe Diem!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 5:37 pm 
Newbie

Joined: Thu Nov 10, 2005 6:05 am
Posts: 16
I see...
then no idea, maybe you can have two other set in both Person and Group classes, I mean Person has a set of Groups in which he is leader, and Group has also a set of Persons which contains it's leaders. This means one more table LeaderPerson_Group.
anyway how do you know in Java code (not in db!) whether a certain person is a leader? or which persons are the leaders of a group?

the mapping I showed was based on the wrong assumption that a group can have only one leader and a Group has a Person field named leader.


Ivan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 7:13 pm 
Newbie

Joined: Fri Oct 14, 2005 8:37 am
Posts: 10
Location: University of Edinburgh
fggarcia wrote:
Someone tell me that its necesary map the Person_Group table, any idea how to map that table and get the information?


How do you implement the relations in Java? Does your Person class include a field Collection groups? Does your Group class include fields Collection members and Collection leaders?

Rafael


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 7:30 pm 
Newbie

Joined: Tue Sep 19, 2006 8:28 pm
Posts: 2
Hi,
For any mapping many side of the association you need collection. Could be any Supported collection type such as Set, Map etc.

Hibernate Doc has some good examples have a look at Person and Events example in the documentation.

Normally many-to-many association is broken down into many-1 and 1-many using third table.


Top
 Profile  
 
 Post subject: Re: Many-to-Many relatinships with attributes,.. how to map
PostPosted: Thu Nov 09, 2006 6:25 am 
Beginner
Beginner

Joined: Mon Nov 06, 2006 2:40 am
Posts: 29
Location: New Delhi, India
fggarcia wrote:
Person_Group is not mapped to a class, because I'm using Collections to hold the relationship, but I cant't map the is_leader column. Now I need querys like "the leaders for a group" or "which groups lead a person"

Do I need map person_group table with a class? or it's possible do that queries without extra mapping?


You definitely need to map third table (the association table person_group) to retrieve data present in person_group table.

Add one to many relation in Person table to person_group table

and in personGroup.hbm create many to one relation with group table

like

person.personGroups.groupDetails
person=original person class
personGroups = Set of person_group table
groupDetails = Object of original group class


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 5:19 pm 
Newbie

Joined: Mon Nov 27, 2006 1:17 pm
Posts: 13
Hey ,
Can somebody tell me how do i go in for many-to-many mapping.
Im actually getting confused with the many-to-many mappings . I have tried two different ways to map.
First of them is the one using many-to-many tag in the tables being mapped. And then the there is no mapping for the joining table. Each of the mappings contain a set of the other.I am able to insert data successfully in this one but retrieval throws an error of "collection not initialized"
Second is the one using one-to-many mappings with the joining table from both tables. And mapping the joining table which has has a composite primary key, which in turn is the combination of foreign keys from each of the end tables. Then I had the make a separate hbm.xml for the composite-id.Retreival works fine here but insertion throws an error of "error in PK composite key id declaration".

Kindly help me in taking the right turn.
Which way is the right one.
And then the right way to access or modify the data from the objects.
I have tried to search into millions of sites and forums, but Im not able to find out a complete solution for that.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 01, 2006 12:27 am 
Beginner
Beginner

Joined: Mon Nov 06, 2006 2:40 am
Posts: 29
Location: New Delhi, India
refer the link
http://www.hibernate.org/118.html#A10

for many-to-many mapping when intermidiate(association) table has extra fields.

if association table has only two fields (one from both tables) then making set of other table in each hbm is correct, and no need to make hbm for association table.(refer documentation)


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