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);
}