This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell 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


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
 
GeeCON Prague 2014
 
subject: delete cascade problem