Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Getting stuck with persisting a M:M relationship

Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been having moderate success with most of my Hibernate mappings so far (the one caveat on moderate is that I think Hibernate is adding some un-needed outer joins to some fetches, that I'll need to look into at some point)... However, I've been running into some problems with a Many:Many mapping.

The Java end of things seems to be correct; the database tables should be correct. But I'm not sure about my mapping/cascade logic. Its a uni-directional M:M. When I persist the Event object, the collection of Dances are not persisted. What am I missing?

Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still stumped on this, but I've made some progress.

I've gone back and forth several times between using an explicit join entity(ie having symmetrical ManyToOne's attached to it from both ends of the real relationship) versus the plain M:M.

It seems to be almost working, or at least its no longer silently failing as before. The code is still as shown above, however, there's no longer braces around the joinColumn/inverseJoinColumn values.

I'm testing with a rather simple (but not too useful test)

On the call to makePersistent, I get an exception:

"Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist:"

I did a few web-searches and found a few bug reports against (which is what I had been using), that indicated there was a regression from 3.2.0 when creating or updating entities on one side of a M:M if you __replaced__ the collection valued attribute (rather than removing/adding the relevant items). So I switched to the getDances() based calls show above in the test code, instead of the former approach of building a new List and then calling setDances(newList). I've also tried upgrading to the newest version No luck. Finally I tried downgrading to the version that was reported clear of the issue I thought I was facing. However it still doesn't work, so I suspect I must be in some slightly different error mode.

Can anyone help me?
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic