-->
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.  [ 2 posts ] 
Author Message
 Post subject: deleting a parent, constraintviolationExc. & version pro
PostPosted: Fri Apr 14, 2006 3:58 am 
Newbie

Joined: Fri Apr 14, 2006 3:31 am
Posts: 1
I am using hibernate 3.05 and postgresql 8.0

I have users table and a userdetails table. the relation users.hbm.xml is

[code] <set name="userdetailses" lazy="false" inverse="true" cascade="all">
<key>
<column name="userid" length="50" not-null="true" />
</key>
<one-to-many class="com.arete.temsa.hibernate.Userdetails" />
</set>[/code]

I use versioning. I create a version field in all my tables. and I define it like that

[code]<version name="version" column="version" type="long"></version>[/code]

Now to my question. I get a List of Users object with Hibernate and send them jsp. In my jsp I choose to delete one of the users. I send the whole Users information to my Servlet ( actually I use spring, hibernatedaosupport ) I regenarate the Users object and try to delete but I got an constraintviolationexception due to foreign key from users to userdetails.

If I load the Users object with load( String key ), and then try to delete, it is succesful, however this time I cannot make version control.

I first load the users Object with

[code]Users obj = usersDao.get( usersArray[i].getUsername() );[/code]
then try to set the verison with
[code]obj.setVerson( usersArray[i].getVersion() );[/code]
then try to delete
[code]usersDao.delete(obj);[/code]
deletion is succesful however I cannot actually set version of obj with above obj.setVerson( usersArray[i].getVersion() );, I try to test it with
obj.setVerson( new Long(45) ); where actual version should be 0, I expect a objectstaleexception, however I see that delete operation resets the version to 0, so no Exception is thrown.

here is the usersDao.delete function

[code] public void delete( Object obj ) throws Exception
{
if ( obj==null )
return;
try{
getHibernateTemplate().delete( obj );
getHibernateTemplate().flush();
}catch( Exception e )
{
getHibernateTemplate().clear();
throw e;
}
}[/code]

At the hibernate logs, deletion is made according to version and key

[code]Hibernate: update public.users set version=?, password=?, enabled=? where username=? and version=?
Hibernate: delete from public.users where username=? and version=?[/code]

I think setting the obj version does not help for verisoning. Any ideas for versioning in delete operation for parent.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 14, 2006 9:22 pm 
Regular
Regular

Joined: Wed Jul 07, 2004 2:00 pm
Posts: 64
Maybe not the nicest approach, but why not do something like this:
Users obj = usersDao.get( usersArray[i].getUsername() );
if (obj.getVersion()!=usersArray[i].getVersion()) {
throw exception
}
usersDao.delete(obj);

On the other hand, if you've decided that the user is to be deleted, do you really care if the version number is wrong?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.