views:

545

answers:

2

hello good people.I'm still learning what hibernate can do and this time i'm trying something that seems not to be working. i have 2 tables users and contacts.as you can guess contacts hold the relationship by have user_id as foreign key. here are snippet of the 2 mapping files. this first is from users.hbm.xml

<set name="contactsdetails">
  <key column="userId"/>
  <one-to-many class="Domain.Contacts"/>
</set>

and this one is from contacts.hbm.xml

<many-to-one class="Domain.Users" name="userId"/>

while every thing is working i mean at configuration,mapping file side and inserting into users table from UsersDAO, i'll like to insert into users and contacts at the same. Meaning i create my users object and assigning values to various properties, create one or array of contacts and assinging various properties to it to and finally add it to the contactdetails set property of users before i save the users objects.when i tried this i realised that it's kinda weird because the contact userId property is of the type users so i'll add user to contacts or each contacts object and then add the same contacts object to the contactsdetails property of users before i persist users objects.i'm sure i'm missing something and i'm having nullpointerexcpetion when i tried.Can you please show me how to do it? thanks for reading

+1  A: 

When you have entity A pointing at entity B, and entity B pointing at entity A, you have what hibernate calls a bi-directional mapping. These can be tricky, and you have to be careful, and also tell hibernate that one side "owns" the relationship.

Have you read the relevant part of the hibernate docs on how to manage these associations?

skaffman
yes i've read that tutorial.i use user and contact to illustrate my problem.let's take for example a case where there is a message table and report table that store recipients number or email and message status.that case the relationship is one to many because a single message unique message is sent to multiple recipients (correct me if i'm wrong). my case is similar to that one.thanks for reading
black sensei
A: 

You need to tell Hibernate which side "owns" the relationship. Normally I find the many-to-one side is simplest. To do this add inverse="true" to one side of the mapping.

<set name="contactsdetails" <!---->inverse="true"<!---->>
  <key column="userId"/>
  <one-to-many class="Domain.Contacts"/>
</set>

From the documentation:

Changes made only to the inverse end of the association are not persisted. This means that Hibernate has two representations in memory for every bidirectional association: one link from A to B and another link from B to A. This is easier to understand if you think about the Java object model and how a many-to-many relationship in Javais created:

category.getItems().add(item);          // The category now "knows" about the relationship
item.getCategories().add(category);     // The item now "knows" about the relationship

session.persist(item);                   // The relationship won't be saved!
session.persist(category);               // The relationship will be saved

The non-inverse side is used to save the in-memory representation to the database.

Rich Seller