-->
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: delete problem
PostPosted: Tue May 03, 2005 11:07 am 
Beginner
Beginner

Joined: Tue Feb 15, 2005 8:14 am
Posts: 32
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.2

How to delete an object with not-null="true" properties ?

I have a uset table where the user name is not-null="true"

I would like to delete the user without getting it from the db first.

I try :

UserBean userBean = new UserBean();
userBean.setId(new Long(1));
try {
session.delete(userBean);
session.flush();
} catch (StaleStateException e) {
// TODO
} catch (ConstraintViolationException e) {
// TODO
} catch (HibernateException e) {
throw new WrappedException(e);
}

but hibernate complains that the user name is null...

I can get it to work by doing this :

UserBean userBean = new UserBean();
userBean.setId(new Long(1));
userBean.setName("just something");
try {
session.delete(userBean);
session.flush();
} catch (StaleStateException e) {
// TODO
} catch (ConstraintViolationException e) {
// TODO
} catch (HibernateException e) {
throw new WrappedException(e);
}

or by doing this :

UserBean userBean = userBean = (UserBean) session.get(UserBean.class, new Long(1));
try {
session.delete(userBean);
session.flush();
} catch (StaleStateException e) {
// TODO
} catch (ConstraintViolationException e) {
// TODO
} catch (HibernateException e) {
throw new WrappedException(e);
}


What is the best way to do it ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 03, 2005 11:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
If UserBean is proxiable use load(), not get(), followed by the delete()


Top
 Profile  
 
 Post subject: proxiable
PostPosted: Tue May 03, 2005 11:36 am 
Beginner
Beginner

Joined: Tue Feb 15, 2005 8:14 am
Posts: 32
I am quit new to hibernate, what do you mean by proxiable ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 03, 2005 11:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
http://www.hibernate.org/hib_docs/v3/reference/en/html_single/


Top
 Profile  
 
 Post subject: load ??
PostPosted: Tue May 03, 2005 11:47 am 
Beginner
Beginner

Joined: Tue Feb 15, 2005 8:14 am
Posts: 32
You say I should use load, but when I read the documentation for the load is says :

You should not use this method to determine if an instance exists (use get() instead). Use this only to retrieve an instance that you assume exists, where non-existence would be an actual error.

And the there is no check on that the object should exist .


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 03, 2005 12:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Sure, use get() if you want to actually have a select prior to the delete. Perhaps I'm just overly performance minded.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 03, 2005 12:13 pm 
Beginner
Beginner

Joined: Tue Feb 15, 2005 8:14 am
Posts: 32
So I put this in my UserBean class :

@hibernate.class
table = "USER"
proxy="UserBean"

and delete a user like this :

try {
UserBean userBean = (UserBean) session.load(UserBean.class, theUserToDelete.getId());
session.delete(userBean);
session.flush();
} catch (StaleStateException e) {
throw new ObjectNotFoundException(theUserToDelete);
} catch (ConstraintViolationException e) {
throw new DatabaseException(TextResource.getText("DELETE_CONSTRAINT_VIOLATION_EXCEPTION"), theUserToDelete);
} catch (HibernateException e) {
throw new WrappedException(e);
}


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.