-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Application transaction and collection modification
PostPosted: Tue Sep 20, 2005 7:39 am 
Newbie

Joined: Tue Sep 20, 2005 6:16 am
Posts: 2
Hi,

I use Hibernate and everything is fine. I need to know if there is a trivial solution to my probllem.

In an application transaction - session-per-request-with-detached-objects

I have the following scenario.

req1.
Group group = session.get(Group.class, idGroup);
Person person = new Person(idPerson);
group.getPersons().add(person);
session.save(person);
session.save(group);
session.close();
req.getSession().addAttribute("ddd", group);

req2.
Person person = session.get(Person.class, idPerson);
group.getPersons().remove(person);
session.close();

req3.
// here we create a transcient unsaved object with the same ID as
// the ID of the object in the DB but not removed yet - the user
// modifications are not finished yet
Person person = new Person(idPerson);
group.getPersons().add(person);
session.close();

req4.
session.update(group);
// or
session.merge(group);

When we try to save the modifications there is a constraint violation - we attempt to
insert an object that is already stored in the DB. Actually the remove statement is
not executed.

I would agree that this is how hibernate should work but can you tell me a smart way to solve the problem. I am working on the interceptor and will try to tell the hibernate that this is a detached object and only update will be made.

Thank you in advance.

I met the problem in a much more complex situation and I am lookong forward to solve it.


Top
 Profile  
 
 Post subject: few questions
PostPosted: Tue Sep 20, 2005 12:05 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
1. Do you call transaction.commit() in your code?
2. Have you specified cascade="all" in group-persons relationship?
3. Does Person class redefines equals() and hashcode() so it really get removed from collection on remove (aPerson)?

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: About commit, cascades and etc.
PostPosted: Wed Sep 21, 2005 2:26 am 
Newbie

Joined: Tue Sep 20, 2005 6:16 am
Posts: 2
Well, everithing about cascades, and etc. is written and made to work just fine.

Actually the code fails in just one case - when I remove the person and then add a new unsaved transcient instance with THE SAME ID.
If I simply remove person, or add a persistent person or add a new unsaved trancient user with ID not stored in the DB everything is fine, fine, fine up to perfect.

The only problem arises when I remove a persistent object from the collection and later on add a new transcient instance with the same ID.
Hibernate checks the version number of the newly added object and finds the unsaved value - in my case negative. Then it tries to insert a new row. But on the previous removal the delete statement is missed because the group is detached and of course not savet - this is application transaction, wrigtht ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.