This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!