• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

delete cascade problem

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}


 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check your private messages.
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic