简体   繁体   中英

working with one-to-many relationship in hibernate

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

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.

See the Bidirectional associations section of the hibernate docs on how to manage these associations.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM