-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: cascading deletes
PostPosted: Wed May 03, 2006 4:08 pm 
Beginner
Beginner

Joined: Thu Mar 09, 2006 1:45 pm
Posts: 26
I am having issues with the cascading deletes with hibernate. I have changed the cascade from all, delete and save-update and whenever I just try and delete the Parent, I get a constraint error listed below. What am I doing wrong?



I have a parent class that can have 1 to many children, the relationship between the two is listed below Parent.hbm.xml file is listed below

<set name="products"
fetch="select"
lazy="false"
inverse="true"
cascade="save-update">
<key>
<column name="companyId"/>
<column name="locationId"/>
</key>
<one-to-many class="org.hibernate.testing.Product"/>
</set>

The Childs hbm.xml file relationship is listed here.

<many-to-one name="company" class="Company" insert="false" update="false">
<column name="companyId"/>
<column name="locationId"/>
</many-to-one>





Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute update query
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.hql.ast.UpdateStatementExecutor.execute(UpdateStatementExecutor.java:99)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:297)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:871)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:89)
at org.hibernate.testing.Main.deleteCompany(Main.java:159)
at org.hibernate.testing.Main.main(Main.java:59)
Caused by: java.sql.SQLException: ORA-02292: integrity constraint (ALLANBRONSON.FK4AFD4ACE8245441) violated - child record found

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:955)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1169)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3368)
at org.hibernate.hql.ast.UpdateStatementExecutor.execute(UpdateStatementExecutor.java:76)


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 4:17 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Code:
<set name="products"
    fetch="select"
    lazy="false"
    inverse="true"
    cascade="all-delete-orphan">
....
</set>


Changing the cascade option to all-delete-orphan should work.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 4:44 pm 
Beginner
Beginner

Joined: Thu Mar 09, 2006 1:45 pm
Posts: 26
That worked with I did the code like this:

try {
tx = s.beginTransaction();

Company comp = (Company) s.get(Company.class, cPk);
if (comp == null)
throw new IllegalArgumentException(
"No company for the given id: " + cPk.toString());
s.delete(comp);

tx.commit();



It did not work when I did this way. The reason I am doing it this way, is that I have a Timestamp attribute ( lastTranscation) for Company and I want to pass in a Timestamp value and delete all companies that have a lastTransaction value that are before the passed in value.

Session session = factory.openSession();
Transaction tx = session.beginTransaction();

String hqlDelete = "delete Company where lastTransaction < :delTransaction";
int deletedEntities = session.createQuery( hqlDelete )
.setTimestamp( "lastTransaction ", delTransaction)
.executeUpdate();
tx.commit();
session.close();


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 5:13 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Was there any exception when you tried the second approach?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 5:29 pm 
Beginner
Beginner

Joined: Thu Mar 09, 2006 1:45 pm
Posts: 26
I got this exception from this code

Session session = factory.openSession();
Transaction tx = session.beginTransaction();

String hqlDelete = "delete Company where locationId = :delLocationId and companyId = :delCompanyId";
int deletedEntities = session.createQuery( hqlDelete )
.setLong( "delLocationId", locId )
.setLong( "delCompanyId", compId )
.executeUpdate();
tx.commit();
session.close();




Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute update query
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.hql.ast.UpdateStatementExecutor.execute(UpdateStatementExecutor.java:99)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:297)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:871)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:89)
at org.hibernate.testing.Main.deleteCompany(Main.java:173)
at org.hibernate.testing.Main.main(Main.java:59)
Caused by: java.sql.SQLException: ORA-02292: integrity constraint (ALLANBRONSON.FK4AFD4ACE8245441) violated - child record found

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:955)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1169)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3368)
at org.hibernate.hql.ast.UpdateStatementExecutor.execute(UpdateStatementExecutor.java:76)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.