File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Getting stuck with persisting a M:M relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Getting stuck with persisting a M:M relationship" Watch "Getting stuck with persisting a M:M relationship" New topic

Getting stuck with persisting a M:M relationship

Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
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

Joined: Dec 14, 2004
Posts: 194
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 agree. Here's the link:
subject: Getting stuck with persisting a M:M relationship
It's not a secret anymore!