-->
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: session.delete(object) not working properly
PostPosted: Mon Aug 06, 2007 10:13 am 
Newbie

Joined: Mon Aug 06, 2007 10:00 am
Posts: 7
Hi All,

I am using Hibernate3.2.3. I am facing problem while deleting an object which is having composite key. When I call session.delete(object) method with with wrong set of composite key( it means that composite key is not in the database) I dont get any exception. Unless and untill I dont get exception I wont be able to know whether my deletion was successful or not. If I pass correct composite key, things happen normally and that row is getting deleting. Below is my code.

public boolean deleteUser(AppUserProfile user) throws Exception{
try{
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
sysLog.info("About to delete a user with User Name: "+user.getUserName());
session.delete(user);
session.flush();
session.getTransaction().commit();
return true; // means delete successfull
}catch (Exception e) {
session.getTransaction().rollback();
sysLog.finer(e.getMessage());
throw e;
}
}

Can anyone tell me y is it happening? When I tried doing samething with single primary key, I got exception, so in this it is working in expected way.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 11:58 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
It appears you're trying to delete a transient object i.e. not associated with the current session. If you made it managed first by calling merge(user) you'll get an exception when the user has already been deleted.

The difference in handling between a single id (e.g. a Long) and a composite id results from the inability to define a "not-saved" value for the composite.

Hibernate can be sure that an object with a Long id that isn't null (i.e. Long's not-saved value) is a transient object. As such it tries to delete it and fails - you get an exception.

With the composite id its not so clear. It could equally be a transient or a new, unsaved object. To decide which, hibernate attempts to load the object from the cache, then the database. This returns nothing in your case because its already deleted so hibernate assumes its a new object and silently discards it (as it should for a new object).

Why don't you just switch to Long as a surrogate primary key?


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.