-->
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: One to Many relationship
PostPosted: Tue May 13, 2008 10:08 am 
Newbie

Joined: Tue May 13, 2008 8:59 am
Posts: 2
In a traditional 1 to Many relationship it might be modelled like this:

An Organization has many contacts.

Organization
-------------
Org_Id (PK)
Other fields


Org_Contact
--------------
Contact_ID (PK)
Org_Id (FK)
Other fields


I am being told by a colleague that mapping this in Hibernate will create a problem where the Organization object will be created as a child of the contact instead of the other way around, and that as a result that many resources will be wasted.

He is instructing me to instead build many 1-1 relationships:

Organization
-------------
Org_Id (PK)
Primary_Contact (FK)
Head_Of_Org_Contact (FK)
Billing_Contact (FK)


Org_Contact
--------------
Contact_ID (PK)
Other fields


I find this difficult to believe since a 1-M relationship is the most common relationship found in database design.

Is this really an issue with Hibernate?

Thanks again,

Paul Gomes


Top
 Profile  
 
 Post subject: Everything is driven from the .hbm mapping file - so
PostPosted: Tue May 13, 2008 11:14 am 
Newbie

Joined: Tue May 13, 2008 10:01 am
Posts: 8
Have you used this Hibernate tools. Please have it downloaded. It will be handy. You can generate class files from the mapping just in a click. This way you can see yourself how Hibernate interprets things.

Your friend might be talking about something else.

Other tips:

First of all, I think Organization Contact is not a child of Organization. It can be said as a component of the Organization. However the way you have its own id makes people think this way. And the way you are moving all its columns, without any complaint, into the organization makes me to think like you have no problem in seeing organizationContact as a component of the Organization.

Anyways, option1 assumes you want a separate object (or entity) and its own lifecycle for organizationContact.

Option 1:

Java classes [Bi directional association]:

Code:
pub class Organization {
  Integer orgId;
  Set organizationContacts;

   /*  convenience method - to maintain Java references intact */
  pub void addOrganizationContact (OrganizationContact orgContact) {
      orgContact.setOrganization(this);
      organizationContacts.add(orgContact)
    }
}

pub class OrganizationContact {
  Integer contactId;
  Organization organization;
  getter setter, etc
}

Organization.hbm.xml

Code:
<class name="Organization" table="organization">
  . . . .
  <!-- to access orgContacts from Organization -->
  <set name="orgContacts" inverse="true">
    <key column="org_id" />
    <one-to-many class="OrganizationContact" />
  </set>
</class>


OrganizationContact.hbm.xml

Code:
<class name="OrganizationContact" table="organization_contact">
  . . . .
  <id name="contact_id">
    <generator class="..." />
  </id>
  <!-- to enable access from this side; access organization from an organizationContact-->
  <many-to-one name="organization" column="org_id"
          class="Organization" not-null="true" />
  . . . .
</class>



Option 2:

If you really do not want to access organization from organization contact objects, in other words we just care about association direction from organization to organizationContact and not the other way around, then Organization Contacts can be treated as components of Organization.

CLASS FILES:
Code:
public class Organization {
  .....
  Set orgContacts ..;
  ....
}

public class OrganizationContact {
  just other fields;
  String contactName;
}



MAPPING FILES:

Code:
<class name="Organization" table="organization">
  <set name="orgContacts" table="org_contact" >
    <key column="org_id" />
    <composite-element class="OrganizationContact">
      <property name="contactName" column="contact_name" not-null="true" />
        . . . .
    </composite-element>
  </set>
</class>

Every property of the component should be not-null (while doing persistence decisions [insert or update], hibernate needs to compare copies).

In this Option2, there will NOT be any mapping file for OrganizationContact since it is a component of Organization, it has no object life on its own, it is a value type, it does not have its own id.


Please refer some books for other options or try out things with Hibernate Tools.

_________________
Best wishes


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.