This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Hibernate collection mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Hibernate collection mapping" Watch "Hibernate collection mapping" New topic

Hibernate collection mapping

Stan Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 120

May I ask few questions about collection mapping using hibernate framework.
Some of them are very general:
1. What is the best way to map the following relation model
member -> orders (one to many)

Problem of this mapping is that it is slow. But if I switch to lazy="true"
(that dramatically increase a perfomance) then in certain cases I could
not delete orders because I got exception that "there is more then one
copy of the collection). Is it known problem ?

2. I have a class that has only 3 fields that makes composite primary key (special thanks to our DBA). Application fails in case of the following scenario
1 get object
2 delete object
3 create the same object (with the same primary key)
4 commit -> exception
But if add session.flush after step 3 then everything goes well. I use SQL server. Could it be because hibernate tries to update primary key columns that is not allowed to do by MS SQL server ?

Good luck for yourself.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You might try posting your questions in the Object Relational Mapping forum as that's where the Hibernate cats hang out.

The reason you get an error when creating the object after deleting it in the same Session is because the Session maintains a map of all your modified objects (those passed to save, update or delete), using their PKs as the keys. You end up trying to add the "same" object (because they have the same key) as both "to delete" and "to insert", which Hibernate won't allow.

Calling flush() persists all changes in the Session's cache (unit of work) to the database and empties the cache. This allows you to then add your object again since the deleted one is gone. So the error was not a SQL error but a Hibernate error.

Without seeing your code, I have no idea why you're getting the error "there is more then one copy of the collection." Are you creating your own Set after loading the object? Unfortunately, Hibernate persists changes to Sets by comparing the original to the new one rather than tracking each add/remove call.
I agree. Here's the link:
subject: Hibernate collection mapping
It's not a secret anymore!