-->
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: Deleting orphans on many-to-one
PostPosted: Thu Dec 07, 2006 6:04 pm 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
Hibernate version: 3.2

Hello,

I have read in the documentation that it is not possible to delete orphans by cascade in many-to-one associations.

Is it because there is another way or is it simply not supported.

I work with detached objects. Therefore, if my parent has an association on a child like Person -> Email, and that I delete the email from a Person, I would like that email to be deleted in the database.

To do so, I would like to do:
person.setEmail(null);

When saving the person (which is a detached instance), it would delete the association from the database (which already works) but also the Email instance (which is obviously not the case today).

How can I do this with the detached objects?

Thanks,

Francois


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 4:59 pm 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
By reading more and more I have finally figured out 2 things:

1- My subject is incorrect because what I am really trying to do is a one-to-one relationship. Unfortunately the documentation suggests to use a many-to-one association to "simulate" the unidirectional one-to-one association.

2- There doesn't seem to be a way (with hibernate mappings) to delete the orphans of any many-to-one or one-to-one association.

I have read many posts including these:
http://forum.hibernate.org/viewtopic.ph ... t=onetoone
http://forum.hibernate.org/viewtopic.ph ... e+property

I don't have a problem with using the many-to-one association to "simulate" the one-to-one but I would like to be able to delete the orphan of this type of association when nulling the association:

Code:
person.setEmail(null);
session.save(person);


should result in both the person and the phoneNumber to be deleted from the database

Worst case scenario if anybody has a workaround, it would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 5:01 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
It should work, but you must ensure that you break the relation in both directions :
    - the child from its parent (what you miss)
    - the parent from its child (what you did)


and map the relationship with cascade="...,delete-orphan".

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Last edited by batmat on Fri Dec 08, 2006 5:33 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 5:10 pm 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
Hi,

I'm not sure I understand what you mean by "break the relation in both directions".

Ideally, in my example Email does not and should not know about Person. It should be a unidirectional association Person -> Email.

This being said, if this is the only workaround I'm willing to look into it. Here are my mappings. How would you go in changing them to delete the "Email orphans"?

Code:
   <class name="org.hibernate.bugs.orphandelete.entities.Person" table="PERSON"
      lazy="false" dynamic-update="true">

        <id name="id"
            column="ID"
            type="string"
            length="32">
            <generator class="uuid.hex"></generator>
        </id>

      <property name="name" type="string" update="true"
         insert="true">
         <column name="NAME" />
      </property>

      <many-to-one name="email"
         column="EMAIL"
         class="org.hibernate.bugs.orphandelete.entities.Email"
         cascade="all"
              outer-join="false"
         update="true"
                        insert="true"
                        unique="true"/>
          </class>
   <class name="org.hibernate.bugs.orphandelete.entities.Email"
         table="EMAIL"
         lazy="false"
         dynamic-update="true">

        <id name="id"
            column="ID"
            type="string"
            length="32">
            <generator class="uuid.hex"></generator>
        </id>


      <property name="email"
              type="string"
              update="true"
              insert="true">
         <column name="EMAIL" />
      </property>
   </class>


It is important to note that it is not possible to set "delete-orphan" on a many-to-one or a one-to one association.



Thanks,

François


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 22, 2006 2:47 pm 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
Any ideas anyone?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 22, 2006 5:57 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
<many-to-one> is rarely mapped with cascade="all" because it simply rarely makes sense. For example, it would mean that deleting one particular child would imply deleting its parent, which could have other children?!

On <one-to-one> I can't why you would map it with a cascading relation. But IMO, it would simply imply calling delete() on the item you want to be deleted.

Then, in fact, as said in the ref doc, unidirectional mappings are less easily "questionnable" by OR tool. So mapping if possible a bidirectional relation would certainly simplify your case. If you don't want to allow your users to see one of the side, just put the getter/setter protected on that side: this way your public contract will stay the same, and Hibernate will be able to use the other side information.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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.