• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Hibernate and Spring

 
Eusebio Floriano
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I�m stucked with the following problem:
I have an modified object and i need to update the DB. But i must compare it with the data from the DB to know the differences and log them.
What i�m doing is call a facade method (opening an transaction using spring), ie: myFacadeMethod, and passing the modified object as an argument.
This method (myFacadeMethod) calls an method from DAO to retrive the original object, ie: myDAO.findObjectByID().
Then there are some business rules, ie: comparing the differences, but when myFacadeMethod calls myDAO.updateObject() passing a modified object as an argument, I always get "NonUniqueObjectException: a different object with the same identifier value was already associated with the session: objectID".
I�ve already tryied to update using spring�s way (org.springframework.orm.hibernate.HibernateTemplate.update()) or getting a Session (net.sf.hibernate.Session) and updating it.

What should i do to not get this exception ?

Regards,
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check Session.saveOrUpdateCopy()


Hibernate API saveOrUpdateCopy
Spring API

im not sure this is the way to go ?

cheers

pascal
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class NonUniqueObjectException

This exception is thrown when an operation would break session-scoped identity. This occurs if the user tries to associate two different instances of the same Java class with a particular identifier, in the scope of a single Session.
 
Eusebio Floriano
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Somkiat Puisungnoen:
class NonUniqueObjectException



Thx for the help.
The cause of the exception i know. What i would like is how to solve this problem.

Regards,
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saveOrUpdateCopy()

Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved or does not exist in the database, save it and return it as a newly persistent instance. Otherwise, the given instance does not become associated with the session.


isnt this what you are looking for ?

pascal
 
Eusebio Floriano
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pascal betz:
check Session.saveOrUpdateCopy()


Hibernate API saveOrUpdateCopy
Spring API

im not sure this is the way to go ?

cheers

pascal



I�ve already read the apis but there is nothing there to help solve my problem.

Regards,
 
Eusebio Floriano
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pascal betz:
saveOrUpdateCopy()


isnt this what you are looking for ?

pascal


Hi Pascal,
Actually it�s not ..
If i save the object without get the actual state in DB, i�ll lost the previous state and i couldn�t find out what was altered.
But if i get the object before i save it, hibernate throws that exception.
Did you get what i need ?
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this what you want to do ?

1. start tx
2. load object A
3. close tx


4. change object A (-> Object B)


5. start tx
6. load object A
7. compare A to B
8. store() B
9. close tx

at step 6 you have the object in your session thats why you can not store another object with the same id in step 8.
but if you either evict() A from the session before step 8 or use saveOrUpdateCopy() in step 8, then it should work.

but perhaps i'm longing for the weekend and my brain stopped working some hours ago and i got you completely wrong :-)

cheers

pascal
 
Eusebio Floriano
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pascal betz:
is this what you want to do ?

1. start tx
2. load object A
3. close tx


4. change object A (-> Object B)


5. start tx
6. load object A
7. compare A to B
8. store() B
9. close tx


yeahhhh Pascal. That�s it dude !!

Originally posted by pascal betz:

at step 6 you have the object in your session thats why you can not store another object with the same id in step 8.
but if you either evict() A from the session before step 8 or use saveOrUpdateCopy() in step 8, then it should work.


I didn�t try saveOrUpdateCopy(). I could try it. But i didnt realize why it should work and just save() not.
Another problem is that hibernate�s version that i�m using has a bug reported by Gavin King (hibernate could cause NullPointerException) [/QB]

Originally posted by pascal betz:

but perhaps i'm longing for the weekend and my brain stopped working some hours ago and i got you completely wrong :-)

cheers

pascal


Take a rest dude ..
I still have a loonng day until my deserved weekend .

Regards,
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im not sure if 2.1.4 already supports saveOrUpdateCopy(). just use evict() to remove the Object from the session, then use saveOrUpdate() to store the modified version of your object.


cheers

have a nice weekend....

pascal
 
Luthiano Vasconcelos
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinicius,

Have a look in 'Interceptor' interface... You can use the 'onSave' method to audit changes in database. Boa sorte!

Luthiano Vasconcelos
Fortaleza - CE - Brazil
 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was having this problem (tonight) with hibernate 3 and spring framework.

what i was wanting to do was

- load a collection of mapped beans
- set the collection as nested collection in a struts action form
- the user would manipulate (add, change, delete) the items.
- the saveItem() method would receive the modified collection (of hibernate mapped beans, new ones have null PK) and then:
- get the collection of items in the database (again)
- compare the database version of things with the collection returned from user form. things that are not in the user form's collection are deleted
- take the list of items from the user form, for each item, save it.

code snippet:



the manager class uses spring framework transaction proxy, the dao is mapped using the hibernate dao support.

so the error was happening at the call the dao save() method, where i was getting the wonderful message:

[ June 18, 2006: Message edited by: Travis Hein ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic