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 ?