aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate: Found two representations of same collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: Found two representations of same collection" Watch "Hibernate: Found two representations of same collection" New topic
Author

Hibernate: Found two representations of same collection

Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Hi all,

I'm getting a HibernateException saying "Found two representations of same collection: domain.Email.recipients" when updating a class that's holding a java.util.Set of java.lang.Strings. Doing a Session#save() on the same object works perfect. It's just the Session#update() that's making trouble here.

I tried searching the Hibernate forum and found a couple of old threads that discussed the same problem but there were no solutions offered. (I also tried reading the auto-translated Russian Hibernate forum but no luck there either)

Here's all the relevant pieces:

I'm using Hibernate version 2.1.7c against PostgreSQL 8.0.0-rc4 (Win32).

The mapping document:


Database tables:


The Java class being mapped:


The piece of code doing the failing update:


The stack trace of the exception that gets thrown:


The generated SQL:


And, finally, the debug level Hibernate log excerpt:


Big thanks for reading this far!
[ January 16, 2005: Message edited by: Lasse Koskela ]

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Were there *any* replies to the threads you found? I don't have the slightest idea what that error message even means...

My first idea would be to take a look at the source code where the exception is thrown. Perhaps I will find the time to do that tomorrow at work...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
There were replies alright but none that helped, unfortunately.
I'll also dig inside Hibernate tomorrow. Remind me to report back my findings if you don't hear from me...
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Should I perhaps specify a primary key for the email_recipient table?
E.g.

It just occurred to me that Hibernate might check for this since the semantics of a Set not containing duplicate entries would effectively translate to such a primary key in the database. Does it (check such things)?

I'm not in a place where I could try it out right now so I'll have to report back later after trying that... In the meanwhile, I'd appreciate a confirmation one way or the other, of course.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Should I perhaps specify a primary key for the email_recipient table

Absolutely. I've not seen the error you are getting before, but I'd suspect its because you have a table with no PK. The Hibernate developers are very strict about a relational value requiring a PK, because they (quite correctly) state that without one its not a relational value. Given their assumption, I would imagine that somewhere in their code your model is causing identity problems, hence the error.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The code is a little bit, well, obscure:



I didn't find any comment in an addCollection method, though. :roll: Well, what do you expect from a 3000 line class...

It's doubly confusing because getCollectionEntry just looks it up in a Map and null means that *it wasn't found at all*, as far as I can tell. I don't have the slightest idea what that has to do with *more* than one representation...

Lasse, all I can advice you to do is running the code through your preferred debugger, crossing fingers and execrating the hibernate developers...
[ January 17, 2005: Message edited by: Ilja Preuss ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Okay. I might try some garlic and exorcism if that doesn't help.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Paul Sturrock:
Absolutely. I've not seen the error you are getting before, but I'd suspect its because you have a table with no PK. The Hibernate developers are very strict about a relational value requiring a PK, because they (quite correctly) state that without one its not a relational value. Given their assumption, I would imagine that somewhere in their code your model is causing identity problems, hence the error.

Nope. I added a primary key (email_id, email_recipient) to the table but that didn't help. I also let Hibernate auto-create the table and it didn't work with that either (and it didn't create a primary key for that table, by the way).
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I also downgraded to 2.1.6 and the problem persists.
Denys Mitin
Greenhorn

Joined: Jun 27, 2014
Posts: 1
Ilja Preuss wrote:I didn't find any comment in an addCollection method, though.

You probably looked on wrong version of source code or wrong variant of method.
For me it looks like there is enough information
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10207
    
166

Denys, welcome to CodeRanch!

The thread is 9 year old so it's possible that the version which Ilja looked at didn't have those comments at that time and the newer version probably has it.


[My Blog] [JavaRanch Journal]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate: Found two representations of same collection