This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Hibernate O To M Relationship question.  RSS feed

 
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?
 
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?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!