-->
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: One-to-Many
PostPosted: Fri Dec 08, 2006 3:16 pm 
Newbie

Joined: Thu Dec 07, 2006 3:52 pm
Posts: 7
Location: Vancouver, CANADA
I have something simple: please excuse my use of c#
Code:
public class Person
{
private int m_PersonID
List<Address> m_Addresses;
 
public Person()

}
public int PersonID{get{return m_PersonID;}set{m_personID=value;}}
public List<Address> Addresses{get{return m_Addresses;}set{m_Addresses=value;}}
}
 
public class Address
{
private int m_AddressID
public Address()
{
}
public int AddressID{get{return m_AddressID;}set{m_AddressID=value;}}


I have 3 tables that create this mapping, since i want to support addresses belonging to things other than Person
TABLES: Person, Address, PersonAddress
I have Keys inside the tables that link the 2 classes together via PersonAddress, PersonAddress table contains PersonAddressID, PersonID, and AddressID.
Simple RDBMS
I have seen many examples on the help notes about linking direct ie Person directly Many to One on address, but since address is a property of Person and could also be a property of other classes such as Corporation(), i dont want to tie the classes together directly.
How do I represent this in hbm.xml

Thanks in advance and excuse my ignorance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 4:16 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Hi, I don't have time to write your whole mapping, but I can give you some advice on the steps to follow.

First, have a look in the reference guide : http://www.hibernate.org/hib_docs/v3/re ... sociations

You'll find what to begin with as a mapping. Your case should be handled without much efforts. As I understand, you never used Hibernate before, so just try and begin by mapping only one class without any relationships, save it in the db and check that it's stored.

Then map your three classes and verify you can save them in the db separately (maybe you'll need to disable some db constraints temporarily to do it though). Then come back in this post and provide your mappings, and I'll help you :-).

See you.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 4:20 pm 
Newbie

Joined: Thu Dec 07, 2006 3:52 pm
Posts: 7
Location: Vancouver, CANADA
Will do, there is obviously more than what i just posted here but i willc clean up for simpl;icity sake


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 4:23 pm 
Newbie

Joined: Thu Dec 07, 2006 3:52 pm
Posts: 7
Location: Vancouver, CANADA
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="WSL.ModelObjects.Person,WSL.ModelObjects" table="Person">

<id name="Personid" column="PersonID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="WSL.ModelObjects.Address,WSL.ModelObjects" table="Address">

<id name="Addressid" column="AddressID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="WSL.ModelObjects.PersonAddress,WSL.ModelObjects" table="PersonAddress">

<id name="PersonAddressid" column="PersonAddressID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<many-to-one name="Personid" column="PersonID" class="WSL.ModelObjects.Person,WSL.ModelObjects" />
<many-to-one name="Addressid" column="AddressID" class="WSL.ModelObjects.Address,WSL.ModelObjects" />

</class>
</hibernate-mapping>

there's all three, i didnt put anything in person yet to represent the List<Address>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 6:29 pm 
Newbie

Joined: Thu Dec 07, 2006 3:52 pm
Posts: 7
Location: Vancouver, CANADA
that example was exactly what i was looking for, sorry i posted before looking at it, thanks u so much


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 10, 2006 11:22 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
painlessprod wrote:
that example was exactly what i was looking for, sorry i posted before looking at it, thanks u so much

You're welcome :-)

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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.