Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
[b]Hibernate version:3.0[/b]
[b]Mapping documents:
Parent.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.gloptv.smsc.Parent"
table="parent"
lazy="false"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="id"
column="id"
type="int"
>
<generator class="sequence">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Parent.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="name"
not-null="true"
/>
<set
name="childrens"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
order-by="name"
>
<key
column="parent_id"
>
</key>
<one-to-many
class="com.gloptv.smsc.Children"
/>
</set>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Parent.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Children.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.gloptv.smsc.Children"
table="children"
lazy="false"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="id"
column="id"
type="int"
>
<generator class="sequence">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Children.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="name"
not-null="true"
/>
<many-to-one
name="parent"
class="com.gloptv.smsc.Parent"
cascade="save-update"
outer-join="auto"
update="true"
insert="true"
access="property"
column="parent_id"
not-null="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Children.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
[/b]
[b]Code between sessionFactory.openSession() and session.close():[/b]
[b]Full stack trace of any exception that occurs:[/b]
[b]Name and version of the database you are using:postgresql 8.0[/b]
[b]The generated SQL (show_sql=true):[/b]
[b]Debug level Hibernate log excerpt:[/b]
Hi, I have a class Parent and a class Children. One parent can have many children. My problem is as follows:
For example I delete a children for parent A and immediately output the size of the Set of childrens for parent A. Everything works fine.
Then I try the following:
Parent parent = Parent.getInstance(2);
Children child = Children.getInstance(8);
System.out.println("Nb children before: " +parent.getChildrens().size());
child.delete();
System.out.println("Nb children after: " + parent.getChildrens().size());
That is, I output the size of Set of Children before deleting, delete specfic children and finally output size of Set again. But I got the following exception when it executes the function child.delete():
Exception in thread "main" java.lang.Exception: deleted object would be re-saved by cascade (remove deleted object from associations): [com.
gloptv.smsc.Children#8]
Why is this happening? I've just do parent.getChildrens().size() before deleting and nothing seems to work.
thanks in advance