wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Hibernate- one to many mapping problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate- one to many mapping problem" Watch "Hibernate- one to many mapping problem" New topic
Author

Hibernate- one to many mapping problem

Sagar Bilgi
Ranch Hand

Joined: Apr 26, 2001
Posts: 37
Hi,
I have a classical bidirectional Parent Child relationship in my tables.
Parent has a set of Child Objects and each Child has a reference to the Parent.

This is implemented in the database tables. I used middlegen to automatically read the database schema and generate the mapping files and the POJO's. My problem was that the mappings were generated something like this.

In Parent.hbm.xml :
<set
name="children"
lazy ="true"
inverse="true"
>
<key blah blah>
<one-to-many class blah blah>
</set>

In Child.hbm.xml
<many-to-one
name="parent"
class="ParentClass"
not-null="true"
>
<column name="FOREIGNKEY TO PARENT" />
</many-to-one>

and the POJOs were also generated appropriately. When I called load on the Parent class ( ensuring that the children set was loaded by iterating over the Set - since lazy = true ) I had a live parent object . My assumption was that I should be able to say parent.setChildren(newSetOfChildren) and these changes should be reflected in the database. BUT that did not happen.

Upon reading I found that when you set inverse = true ,those changes are not persisted in the db. The end of the relationship where inverse =false is responsible for persisting it into the database.So I removed the inverse="true" references from the parent and now it seemed to work. Now when I say parent.setChildren(newSetOfChildren) those changes are persisted to the database.

My questions are:
a)There must be a reason why middlegen generated the parent with the inverse=true field.Even in the Hibernate documentation the example that they give has inverse = true for the parent. So Is the change that I did correct? and if not what other repurcussions could it have?

b)What happens if you do not specify inverse =true at any end?

Any clarifications in this will be appreciated.

Thanks
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
So Is the change that I did correct?

only you can answer that.

and if not what other repurcussions could it have?

well you can only persist from one end, which end is your choice.

b)What happens if you do not specify inverse =true at any end?

never tried it, but I can only assume that either it won't persist at all or hibernate will use a default, perhaps the same default used by middlegen.
 
 
subject: Hibernate- one to many mapping problem
 
Similar Threads
what changes should i made in save method in one-to-many relationships
Cascading update problem
retrieving object hierarchy - many-to-one?
Hibernate - Force foreign key lookup on insert
Hibernate One-to-Many foreign key