-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problems with onDelete
PostPosted: Sat Dec 20, 2003 7:28 pm 
Newbie

Joined: Sat Dec 20, 2003 7:18 pm
Posts: 3
Hi..

I'm having a problem with onDelete method in Hibernate 2.1. My onDelete method needs to do a query with other entities, with cause an autoflush.
As a consequence of this, the parent entity is deleted before I can delete the child entities. The full stacktrace below.
The tables "recurso" and "agenda" are the parent entity (joined-subclass in this case). The onDelete method of Recurso is

Code:
(228) try {
(229)   s.delete("select p from Permissao p where p.recurso = ?", this, Hibernate.entity(getClass()));
(230) } catch (HibernateException e) {
(231)   throw new CallbackException(e);
(232) }
(233) return false;


Am I doing something wrong, or is this a bug?

Thanks,
Robson Paniago

21:11:49,385 INFO [STDOUT] Hibernate: delete from recurso where id=?
21:11:49,385 INFO [STDOUT] Hibernate: delete from agenda where id=?
21:11:49,385 WARN [JDBCExceptionReporter] SQL Error: 350, SQLState: 23000
21:11:49,385 ERROR [JDBCExceptionReporter] [350]: Referential integrity violated:FKB307E11599C04A2E,CAMPUS,EVENTO
21:11:49,385 ERROR [JDBCExceptionReporter] Could not synchronize database state with session
com.sap.dbtech.jdbc.exceptions.DatabaseException: [350]: Referential integrity violated:FKB307E11599C04A2E,CAMPUS,EVENTO
at com.sap.dbtech.jdbc.packet.ReplyPacket.createException(ReplyPacket.java:69)
at com.sap.dbtech.jdbc.ConnectionSapDB.throwSQLError(ConnectionSapDB.java:763)
at com.sap.dbtech.jdbc.ConnectionSapDB.execute(ConnectionSapDB.java:429)
at com.sap.dbtech.jdbc.ConnectionSapDB.execute(ConnectionSapDB.java:320)
at com.sap.dbtech.jdbc.CallableStatementSapDB.execute(CallableStatementSapDB.java:380)
at com.sap.dbtech.jdbc.CallableStatementSapDB.execute(CallableStatementSapDB.java:289)
at com.sap.dbtech.jdbc.CallableStatementSapDB.executeUpdate(CallableStatementSapDB.java:697)
at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:308)
at net.sf.hibernate.persister.NormalizedEntityPersister.delete(NormalizedEntityPersister.java:606)
at net.sf.hibernate.impl.ScheduledDeletion.execute(ScheduledDeletion.java:22)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2100)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2066)
at net.sf.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:1566)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1372)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1332)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1322)
at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1457)
at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1447)
at unb.campusvirtual.ejb.dataobjects.Recurso.onDelete(Recurso.java:229)
at unb.campusvirtual.ejb.dataobjects.Agenda.onDelete(Agenda.java:54)
at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:977)
at unb.campusvirtual.ejb.impl.ControleGerenciamentoRecursosBean.excluiRecurso(ControleGerenciamentoRecursosBean.java:189)
....
Code:
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2003 7:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I dont think it is intended to to a delete query in an onDelete of another entity. Can't you use cascade="delete" for this?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 20, 2003 7:36 pm 
Newbie

Joined: Sat Dec 20, 2003 7:18 pm
Posts: 3
This is only a part of the delete logic, this part could really be solved by using cascade. But after this, I need to update some other objects, and, in this case, I really need to do a query.

I can work around this problem by not using Hibernate's Lifecycle interface, but I think that using it I get a cleaner code (I do not need to recurse for every child, for example).

Thanks,
Robson Paniago


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 21, 2003 1:02 pm 
Newbie

Joined: Sat Dec 20, 2003 7:18 pm
Posts: 3
Hi..

I just checked with Hibernate 2.1 and cascade="all-delete-orphan", it worked.

However, using my old onDelete() implementation on class Agenda, it did not work (it tried to delete the parent first), only using the cascade="all-delete-orphan" in the .hbm.xml file worked. My onDelete() was:

Code:
Set eventos = getEventos();
for (Iterator iter = eventos.iterator(); iter.next;) {
Evento obj = (Evento)iter.next();
iter.remove();
session.delete(obj);
}


Is this supposed to work, right?

Thanks
Robson Paniago


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2003 12:10 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
I had the same problem with onDelete. I got rid of the exception by flushing the session, to force the sql deleting the children to be executed before that deleting the parent. This works when I delete 1 parent at time.

But when I delete many parents with: session.delete("from Parent") I get the following exception:

java.lang.IllegalArgumentException: fromIndex(1) > toIndex(0)
at java.util.SubList.<init>(AbstractList.java:706)
at java.util.RandomAccessSubList.<init>(AbstractList.java:860)
at java.util.AbstractList.subList(AbstractList.java:569)
at net.sf.hibernate.impl.SessionImpl.doDelete(SessionImpl.java:1155)
at net.sf.hibernate.impl.SessionImpl.delete(SessionImpl.java:1114)


Hibernate 2.1 reference documentation specifies:
"onSave(), onDelete() and onUpdate() may be used to cascade saves and deletions of dependent objects. This is an alternative to declaring cascaded operations in the mapping file."


Is there any best practice/guideline on how to implement cascade delete in onDelete method.

Thank you,

mota


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2003 7:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is a change in behavior between 2.1 and 2.1.1. Basically we do not allow flushing from onDelete(). You can do session.setFlushMode(FlushMode.COMMIT) to disable the flush that occurs before query execution.

P.S. I would never, ever do this kind of stuff in on onDelete() method. It is waay to complex....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.