File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "delete cascade problem" Watch "delete cascade problem" New topic

delete cascade problem

noemi conde

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 : 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:

public void deleteTest(){
User user = new User();
UserRol userRol1 = new UserRol();
Set<UserRol> usersRol = new HashSet<UserRol>();

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

Joined: Apr 14, 2004
Posts: 10336

Please check your private messages.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
I agree. Here's the link:
subject: delete cascade problem
It's not a secret anymore!