-->
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.  [ 7 posts ] 
Author Message
 Post subject: Deleting transient object with empty not null property
PostPosted: Wed Oct 25, 2006 5:11 am 
Newbie

Joined: Wed Oct 25, 2006 4:59 am
Posts: 4
Hi!

I have transient object created by the new operator. I've set the id and some of the properties but one property marked as not-null in the mapping file. I've tried to delete it using session.delete(user), but it fails:

Code:
org.hibernate.PropertyValueException: not-null property references a null or transient value: ...


Why it is needed to have all not-null properties set when I delete an object?

Hibernate version: 3.1.3


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 5:18 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Why would you want to delete a transient object? This makes no sense to me.

The Hibernate Documentation defines transient as follows:


Transient - an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application doesn't hold a reference anymore. Use the Hibernate Session to make an object persistent (and let Hibernate take care of the SQL statements that need to be executed for this transition).


There is absolutely no sense in deleting an object that is transient.

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 5:24 am 
Newbie

Joined: Wed Oct 25, 2006 4:59 am
Posts: 4
MikePloed wrote:
Why would you want to delete a transient object? This makes no sense to me.


Actually I now there's an entity in the database with the given ID. I could load it, and delete it, but I can simply delete it when it's a transient object if every not-null properties were set.

It was a persistent object, but written to XML. After a while I read the XML and I want to delete it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 5:33 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
I think you should consider using the bulk delte functionality of HQL for your use case:

Code:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlDelete = "delete YourEntity c where c.id= :id";

int deletedEntities = s.createQuery( hqlDelete )
        .setString( "id", id)
        .executeUpdate();
tx.commit();
session.close();


This functionality is described here:
http://www.hibernate.org/hib_docs/v3/re ... tch-direct

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 5:41 am 
Newbie

Joined: Wed Oct 25, 2006 4:59 am
Posts: 4
MikePloed wrote:
I think you should consider using the bulk delte functionality of HQL for your use case:


Thanks for your help, but what I'm asking is why it is necessary to have all not-null proprties set when I want to delete an object? It makes no sense IMHO.

((To solve the problem I've already modified my code to load the object before delete, it isn't a big performance issue.)

nOR


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 5:46 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Well, the sense of delete is, that you have already loaded the object. So all not-null properties are guaranteed to be present. You usually don't delete transient objects.

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 6:06 am 
Newbie

Joined: Wed Oct 25, 2006 4:59 am
Posts: 4
MikePloed wrote:
Well, the sense of delete is, that you have already loaded the object. So all not-null properties are guaranteed to be present. You usually don't delete transient objects.


That's a good point of view. Thanks.


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