-->
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: help in defining many to many mapping
PostPosted: Mon Jul 31, 2006 8:07 am 
Newbie

Joined: Tue Mar 07, 2006 6:54 am
Posts: 2
Hi ..

I was just exploring the powers of hibernate when i came across this scenario "I would like to implement a many to many relation in hibernate. Which is as under :-

1)Person Table
person id ( primary key)

2) Address Table
address id (primary key)

3) Person_Address (many to many mapping table)
person id (foreign key)
address id (foreign key)
count Integer (some dummy column )

classes mapping to these tables are
1) Person.java
2) Address.java
3)PersonAddress.java

Now, i would like to map it in such a way that i can retreive the count column of the person_address table. Most of the mappings tried by me only mapped person and address and i wasnt able to get the "count" information from person_address table. Can anyone help me to acheive this ?

Also find the sample mapping i used


Hibernate version: 3.1.13

Mapping documents:

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses"
table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"
unique="true"
class="Address"
/>
</set>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<join table="PersonAddress"
inverse="true"
optional="true">
<key column="addressId"/>
<many-to-one name="person"
column="personId"
not-null="true"
/>
</join>
</class>

Any pointers on this would be highly appreciated.

Thanks in advance.

_________________
Hibernate Newbie


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 31, 2006 5:54 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You have to map PersonAddress to do that. You can map it as a standalone entity, but I've always thought that a bit over the top. Define it as a named composite element.

If count is a list index (that is, each person gets its own sequence of count) then you can define the mapping as a simple many-to-many using the <list> collection, instead of <set>. This means that you don't have to map PersonAddress explicitly.

If count is unique for all rows in Person_Address, then you can define it to be part of a composite key of a fully mapped PersonAddress entity: that makes for a more complex mapping, but means that hibernate can look after generating the value in the column.

If neither of those options applies, go with the composite element mapping.

_________________
Code tags are your friend. Know them and use them.


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.