• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate collection mapping

 
Ranch Hand
Posts: 120
IntelliJ IDE Hibernate Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic