aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Hibernate collection mapping" Watch "Hibernate collection mapping" New topic
Author

Hibernate collection mapping

Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 117

Hello,
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: http://aspose.com/file-tools
 
subject: Hibernate collection mapping
 
Similar Threads
delete cascade problem
Hibernate - multiple collection with restrictions mapping problem
Hibernate - Force foreign key lookup on insert
Would like advise mapping Hibernate objects
one-to-many relation