-->
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.  [ 7 posts ] 
Author Message
 Post subject: Mixed up with delete child from one-to-many
PostPosted: Tue May 24, 2005 9:49 am 
Newbie

Joined: Tue May 24, 2005 9:20 am
Posts: 5
Hibernate version:
2.1.6

The problem I encounter is in deleting a child from one-to-many. I've been experienced with bidirectional one-to-many and it works fine.
But for some performance reason I decided to use simple one-to-many that child doesn't have any reference to parent. I tested with many configurations of cascade and inverse and I didn’t succeed.


Thanks in advance for your notice!

Mapping documents:
Code:
<class name="Worksheet" table="WORKSHEET">
   <property ...>
   <property ...>
        <bag name="tabsheets" cascade="all-delete-orphan">
            <key column="WORKSHEET_ID"/>
            <one-to-many class="Tabsheet"/>
        </bag>
</class>

<class name="Tabsheet" table="TABSHEET">
   <property ...>
   <property ...>
</class>

Code between sessionFactory.openSession() and session.close():

Using spring HibernateDaoSupport class
just removing a child from parent and calling saveOrUpdate()
The only point remained is my parent object is detached from it's session.

Full stack trace of any exception that occurs:
No Exception
Name and version of the database you are using:

Oracle 10g

The generated SQL (show_sql=true):

Hibernate: update TABSHEET set WORKSHEET_ID=null where WORKSHEET_ID=?
Hibernate: update TABSHEET set WORKSHEET_ID=? where TABSHEET_ID=?
Hibernate: update TABSHEET set WORKSHEET_ID=? where TABSHEET_ID=?
Hibernate: update TABSHEET set WORKSHEET_ID=? where TABSHEET_ID=?

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 5:06 pm 
Newbie

Joined: Fri May 20, 2005 12:39 pm
Posts: 10
By making the relationship unidirectional, you are actually saying that the child can exist independently of its parent. Therefore, when you remove the child from the parent collection and save the parent, an update of the child occurs.

If you want to keep the unidirectional relationship, then you will need to explicitly delete the child.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 12:13 am 
Newbie

Joined: Tue May 24, 2005 9:20 am
Posts: 5
No that's not right!
If you specify a relationship unidirectional you mean u want, parent manage it's child. But what does this mean :

update TABSHEET set WORKSHEET_ID=null where WORKSHEET_ID=?

With any configuration in anay circumstances this sql is not acceptable! Can anyone help me?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 12:38 am 
Newbie

Joined: Tue May 24, 2005 9:20 am
Posts: 5
I solved my problem. I changed my mapping according to below and then directly delete the child.

But any comment on the solution. I think Hibernate has no effect on the solution and i solved it by hand. Because on any one-to-many you will do like this.
And any comment on what is the role of all-delete-orphan.

Code:
<class name="Worksheet" table="WORKSHEET">
   <property ...>
   <property ...>
        <bag name="tabsheets" [color=red]inverse[/color]="true" [color=red]cascade[/color]="all">
            <key column="WORKSHEET_ID"/>
            <one-to-many class="Tabsheet"/>
        </bag>
</class>

<class name="Tabsheet" table="TABSHEET">
   <property ...>
   <property ...>
</class>



I forgot something in my first post :

The generated SQL (show_sql=true):

Hibernate: update TABSHEET set P1=?, P2=?, P3=? where TABSHEET_ID=?
Hibernate: update TABSHEET set P1=?, P2=?, P3=? where TABSHEET_ID=?
Hibernate: update TABSHEET set P1=?, P2=?, P3=? where TABSHEET_ID=?

Hibernate: update TABSHEET set WORKSHEET_ID=null where WORKSHEET_ID=?
Hibernate: update TABSHEET set WORKSHEET_ID=? where TABSHEET_ID=?
Hibernate: update TABSHEET set WORKSHEET_ID=? where TABSHEET_ID=?
Hibernate: update TABSHEET set WORKSHEET_ID=? where TABSHEET_ID=?

and now it's like this:

The generated SQL (show_sql=true):

Hibernate: update TABSHEET set P1=?, P2=?, P3=? where TABSHEET_ID=?
Hibernate: update TABSHEET set P1=?, P2=?, P3=? where TABSHEET_ID=?
Hibernate: update TABSHEET set P1=?, P2=?, P3=? where TABSHEET_ID=?
Hibernate: delete from TABSHEET where TABSHEET_ID=?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 12:40 am 
Newbie

Joined: Tue May 24, 2005 9:20 am
Posts: 5
Sorry For Posting again

I solved my problem. I changed my mapping according to below and then directly delete the child.

But any comment on the solution. I think Hibernate has no effect on the solution and i solved it by hand. Because on any one-to-many you will do like this.
And any comment on what is the role of all-delete-orphan.

Code:
<class name="Worksheet" table="WORKSHEET">
   <property ...>
   <property ...>
        <bag name="tabsheets" inverse="true" cascade="all">
            <key column="WORKSHEET_ID"/>
            <one-to-many class="Tabsheet"/>
        </bag>
</class>

<class name="Tabsheet" table="TABSHEET">
   <property ...>
   <property ...>
</class>



I forgot something in my first post :

The generated SQL (show_sql=true):

Hibernate: update TABSHEET set P1=?, P2=?, P3=? where TABSHEET_ID=?
Hibernate: update TABSHEET set P1=?, P2=?, P3=? where TABSHEET_ID=?
Hibernate: update TABSHEET set P1=?, P2=?, P3=? where TABSHEET_ID=?

Hibernate: update TABSHEET set WORKSHEET_ID=null where WORKSHEET_ID=?
Hibernate: update TABSHEET set WORKSHEET_ID=? where TABSHEET_ID=?
Hibernate: update TABSHEET set WORKSHEET_ID=? where TABSHEET_ID=?
Hibernate: update TABSHEET set WORKSHEET_ID=? where TABSHEET_ID=?

and now it's like this:

The generated SQL (show_sql=true):

Hibernate: update TABSHEET set P1=?, P2=?, P3=? where TABSHEET_ID=?
Hibernate: update TABSHEET set P1=?, P2=?, P3=? where TABSHEET_ID=?
Hibernate: update TABSHEET set P1=?, P2=?, P3=? where TABSHEET_ID=?
Hibernate: delete from TABSHEET where TABSHEET_ID=?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 9:09 am 
Newbie

Joined: Fri May 20, 2005 12:39 pm
Posts: 10
Again, what I told is correct. Think about it this way...

In your configuration, your parent class Worksheet knows about the child Tabsheet BUT Tabsheet knows nothing about Worksheet. There is no way to navigate from Tabsheet to Worksheet. This means that the WORKSHEET_ID column _must_ be nullable in your TABSHEET table because when you remove a Tabsheet from the parent collection, it is going to generate an UPDATE statement.

As I said in my previous post, you either have to make the association bi-directional or manually delete the child.

I would strongly recommend buying a copy of Hibernate in Action as it covers a lot of these sticky spots; see page 233 for the problem above.

Also see the Hibernate FAQ - point 6:
http://www.hibernate.org/116.html#A6


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 12:14 am 
Newbie

Joined: Tue May 24, 2005 9:20 am
Posts: 5
Tim,
First thanks for your notice.

I used bidirectional many times and it works for me. I know how it works and I need uni-directional because of performance. I don't like childs to reference their parent to consume less memory but It's now unavoidable. Deleting a child explicitly is not according to OO rules, So i decided to remain on bidirectional and in the case of performance problem, I will bypass hibernate and use plain jdbc.


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