Hi,
I've on scenario of 1 to M.
Country -> State where a country may have many states.
A country stores a set of States. So in Country.hbm.xml I've
<set name="states" cascade="all">
<key column="COUNTRY_CODE" not-null="true"/>
<one-to-many unique="true" class="com.o2m.model.State" />
</set>
When I create a country with 2 states in
Java code, it fires 1 Country Insert, 2 State inserts and 2 update inserts.
Last 2 update inserts are for maintaining relationship link. It seems inefficient.
So to get over the stuff, I made following changes.
In country.hbm.xml, I said inverse="true" for <set> and for state.hbm.xml I said Many-to-one to country and also introduced a country variable in State.
So effectively I reversed the direction of relationship. Now if I save a country, state doesn't get saved in DB. When i have one to many in country.hbm.xml, why doesn't this work?
Best solution would be if approach 1 can work without having updates. Is there a way to do it?