This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes delete cascade problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "delete cascade problem" Watch "delete cascade problem" New topic
Author

delete cascade problem

noemi conde
Greenhorn

Joined: May 05, 2010
Posts: 14
Hi all,

I have a problem when I try to delete an Object "User" which has a collection of Objects "UsersRol", I cannot delete user because I obtain a DataIntegritiViolationException, (I suppose I'm not deleting on cascade, and I got this exception because, the objects related to user cannot exist in the database without user, but, this is the exact reason to delete all of them on cascade)

This code works fine with the operation merge, but I don't know, if I want to delete an object, this object needs to be loaded by hibernate or in the other hand It just need to set its id.
I also try to set the id's of the usersRol collection, but this is still not working

What is my problem ??? I cannot find it !!

Any idea is welcome, thanks in advance.

Here are my mappings:



when I try to delete an instance of user an sql - exception is thrown :

com.ibm.db2.jcc.a.SqlException: DB2 SQL error: SQLCODE: -532, SQLSTATE: 23504, SQLERRMC: SCHEMA1.USER_ROL.FK_USEROL_USER


This is the hibernate exception:
org.hibernate.exception.ConstraintViolationException: could not delete: [User#3]


And this is the first exception in the stacktrace:

Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@1ab600f] to process 'after' execution for test: method [public void UserDaoTest.deleteTest()], instance [UserDaoTest@2fb002], exception [null]
org.springframework.dao.DataIntegrityViolationException: could not delete: [User#3]; nested exception is org.hibernate.exception.ConstraintViolationException: could not delete: [User#3]


And this is the code I try to execute:

@Test
public void deleteTest(){
User user = new User();
user.setUserId(3);
UserRol userRol1 = new UserRol();
userRol1.setUserRolId(10);
Set<UserRol> usersRol = new HashSet<UserRol>();
usersRol.add(userRol1);
userDao.remove(user);
Assert.assertTrue(true);
}


And this is the remove code:


public void remove(E entity) {
final String currentEntityName = getEntityName(entity);

if (LOG.isDebugEnabled()) {
LOG.debug("Removing entity {}", entity);
}

getHibernateTemplate().delete(currentEntityName, entity);
}

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Please check your private messages.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: delete cascade problem
 
Similar Threads
1-to-M mapping confusion
Exception in Deleting Parent and Child entity
joins in Hibernate Annotation
Association references unmapped class exception with hibernate
does Hibernate support one-to-one bidirectional in subclass?