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 ?
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.