-->
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: delete child when deleting parent
PostPosted: Thu Jul 19, 2007 4:35 am 
Newbie

Joined: Thu Jul 19, 2007 4:18 am
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.2

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="events.Nodo" table="Nodo">
<id name="id" column="Nodo_ID">
<generator class="native" />
</id>
<property name="txt" />



<!-- Inserire chiave esterna dalla stessa classe -->
<many-to-one name="dad" class="events.Nodo"
cascade="save-update" fetch="join" lazy="false" update="true"
insert="true" column="dad" />
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
session.beginTransaction();
Nodo padre=(Nodo) session.load(Nodo.class, new Long(1));
session.delete(padre);
session.getTransaction().commit();


Full stack trace of any exception that occurs:
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:146)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at events.EventManager.delete(EventManager.java:181)
at events.EventManager.main(EventManager.java:43)
Caused by: java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Impossibile cancellare la riga: un vincolo d'integrita' referenziale non e' soddisfatto"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1492)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 9 more


Name and version of the database you are using:MySQL 5.0

I have problems when I want to delete a parent-node of the Tables Node.
the table Node was created like this:
_____________________________________
| ID_NODE | DESCRIPTION | DAD_ID_NODE |
-----------------------------------------------------
| 1 | dad | null |
| 2 | first child | 1 |
| 3 | second child | 1 |
-----------------------------------------------------


When I delete the dad I want to delete the first and the second child.

The relation that I submit in the Node.hbm.xml is:

<many-to-one name="dad" class="events.Nodo"cascade="save-update" fetch="join" lazy="false" update="true" insert="true" column="dad" />

The java class of the table Node Is:

public class Nodo {
private Long id;
private String txt;
private Nodo dad=null;
with all the setter and getter.

I hanven't problem when insert or modify the item in the table, and if I try to delete a node that haven't child the operation is execute correcty.
Thanks of all.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 20, 2007 10:41 am 
Newbie

Joined: Mon Jul 02, 2007 2:12 pm
Posts: 18
Hi,

what about cascade="save-update,delete" ?

-Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 3:21 am 
Newbie

Joined: Thu Jul 19, 2007 4:18 am
Posts: 5
I have tried with cascade="save-update,dalete" but it doesn't work.
The exception is the same : org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

Other ideas?

the dialect that I use is "org.hibernate.dialect.MySQLInnoDBDialect", is this the problem?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 5:57 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
pippo,

Use below property in you mapping file and delete your perent record now.

<set
name="children"
batch-size="100"
lazy="true"
inverse="true"
cascade="save-update,delete">
<key
column="DAD_ID_NODE"/>
<one-to-many
entity-name="events.Nodo"/>
</set>


Here the problem is self-reference, for self reference we can create collection(one-to-many) and many-to-one. Parent has set of child so you should use collection to delete child records when you deleting parent record.

- Shanmugam

Don't forget to give rate, if this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 9:05 am 
Newbie

Joined: Thu Jul 19, 2007 4:18 am
Posts: 5
Can you give me the istruction (or a link to the istruction) for save a new nodes? I don't know how to set the dad when I create a children.
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 24, 2007 1:34 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
Pippo,
Can you give me the istruction (or a link to the istruction) for save a new nodes? I don't know how to set the dad when I create a children.
thanks


While inserting a parent record you don't required to insert a value for column DAD_ID_NODE. while inserting a child record you required to insert a value(parent id) to column DAD_ID_NODE.

You can use many-to-one that is ,
<many-to-one name="dad" class="events.Nodo"cascade="save-update" fetch="join" lazy="false" update="true" insert="true" column="dad" />

to insert parent id for child record.

and use set(one-to-many) to delete a child records when deleting parent record.


So, your hbm should have both many-to-one and one-to-many for self reference.

-Shanmugam.N


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.