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?