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: Newbie Question one-to-many
PostPosted: Thu Dec 07, 2006 5:19 pm 
Newbie

Joined: Thu Dec 07, 2006 3:52 pm
Posts: 7
Location: Vancouver, CANADA
I have something simple:
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 5:57 pm 
Beginner
Beginner

Joined: Wed Nov 29, 2006 5:33 pm
Posts: 28
Location: Chicago, IL
Hello

Creating relationships between two objects in NHibernate does not mean that those objects can not be related to other objects. Your decision on whether to use a many-to-many or a many-to-one should be solely based on whether or not a person can have more than one address.

For instance if companies and people only had one address, you could have three tables and classes: Company, Person and Address. All three have primary keys. Both the Person table and Company table contain a column addressId that has a foreign key defined where the primary key is the Address table primary key column.

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="Savo.SAM.Business" assembly="Savo.SAM.Business">
   <class name="Address" table="`Address`">
      <id name="Id">
         <column name="id" not-null="true" />
         <generator class="identity"/>
      </id>
   </class>
   <class name="Person" table="`Person`">
      <id name="Id">
         <column name="id" not-null="true" />
         <generator class="identity"/>
      </id>
      <many-to-one name="Address" column="addressId" class="Address"/>
   </class>
   <class name="Company" table="`Company`">
      <id name="Id">
         <column name="id" not-null="true" />
         <generator class="identity"/>
      </id>
      <many-to-one name="Address" column="addressId" class="Address"/>
   </class>


You could also throw in a couple of one-to-many inverse relationships on the Address class mapping to see which people and companies use that address. If you wanted both to be many to many relationships, you would need five tables, but it would work just fine. There are also other more complex solutions involving inheritance, but those approaches are usually overkill and only serve to confuse unless absolutely necessary.

_________________
Chuck

Not in the face! Not in the face!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 9:15 am 
Newbie

Joined: Thu Dec 07, 2006 3:52 pm
Posts: 7
Location: Vancouver, CANADA
thnks for your post, but the problem is the fact that there is a one-to-many relationship, a person can have multiple addresses, nor do i want to hardcode the relationship between address and person without a join table because of my aforementioned problem, that an address can be owned by other entitys than Person()


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.