• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate O To M Relationship question.

 
amol deshpande
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
amol deshpande
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was there any undersupply of information?

Anyone?
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remove cascade = 'all'
may be it would solve your problem.

Regards
Raza
 
amol deshpande
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i remove cascade="all", saving a country wouldn't save state.
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you give me the table details?
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic