-->
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: Problem with bidirectional mappings...
PostPosted: Thu May 12, 2005 9:58 am 
Regular
Regular

Joined: Fri Feb 25, 2005 3:02 am
Posts: 71
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


Top
 Profile  
 
 Post subject: Re: Problem with bidirectional mappings...
PostPosted: Thu May 12, 2005 5:34 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
gulshan wrote:
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


I assume that child.delete() is calling session.delete(this) or something like that ?

session.delete() does not modify the Java object. It only deletes the data from the database - assuming you commit the transaction.

If the parent collection has a reference to the child, the child object will still exist.

As a side note: If you cascade deletes from your parent to your child, you don't have to explicitly delete the child. Remove it from the parent collection and update the Parent.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 1:09 am 
Regular
Regular

Joined: Fri Feb 25, 2005 3:02 am
Posts: 71
Thanks for your reply, but even if I remove the child from the parent collection "childrens.remove(children)", I do get this exception.

My question is why do I get this exception if I do "parent.getChildrens().size()" before deleting a specific child.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 1:16 am 
Regular
Regular

Joined: Fri Feb 25, 2005 3:02 am
Posts: 71
Am sorry, when I do parent.deleteChildren() everything works fine. Even if I do a query before.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 2:32 am 
Regular
Regular

Joined: Fri Feb 25, 2005 3:02 am
Posts: 71
One more question, if I delete a parent by doing session.delete(this). Will everything will be fine with respect to db and java objects. As far as I know for database everything will be ok. I would like to know if its the same for the Java Object


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.