-->
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.  [ 13 posts ] 
Author Message
 Post subject: one-to-one association removal
PostPosted: Mon Oct 27, 2003 4:12 pm 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
I have following mappings:

Code:
<class name="Person"  table="PERSON">
   <id name="id" column="PERSON_ID">
      <generator class="sequence">
     <param name="sequence">PERSON_S</param>
   </generator>
     </id>      
    <property name="name" column="NAME"/>
    <one-to-one name="info" cascade="all"/>
</class>

<class name="PersonInfo" table="PERSON_INFO">
   <id name="id">
      <generator class="foreign">
      <param name="property">person</param>
   </generator>
   </id>
  <property name="otherInfo" column="OTHER_INFO"/>
  <one-to-one name="person"  cascade="none"/>
</class>


My question is how to remove personInfo from the person class:
I have tried to do:
Code:
   
   person.setInfo(null);
   session.flush();


but it does not seem to work.

I have tried to delete PersinInfo explicitly and it does work.
But it would be nice to have cascade delete semantic on one-to-one. Is there is any way how to accomplish it ?

Thanks,

Giedrius


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2003 5:05 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
If you want to delete the person_info then I think you need a

session.delete(personInfo);

setInfo(null) should break the association from person to personInfo.

In any case, make sure to use transactions and a commit.

Jeff[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2003 9:57 pm 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
Reason why I posted is to find out if it's possible to have cascading delete on one-to-one association.

And dear Jeff I know that in order to delete object I need to call session.delete().

I looked at Hibernate 2.1 beta 4 source code and cascading style delete is not supprted in Hibernate 2.1

code from net.sf.hibernate.engine.Cascades.java:

Code:
if (child != null)
      {
         if (type.isAssociationType())
         {
            if (((AssociationType) type).getForeignKeyType().cascadeNow(cascadeTo))
            {
               if (type.isEntityType() || type.isObjectType())
               {
                  action.cascade(session, child, anything);
               }


so statement person.setInfo(null) is not going to delete info by specifyin cascade style="all"

I believe it's a bug


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2003 10:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
please read the Hibernate documentation before using the word "bug".

It is incredibly explicit on this point.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 9:23 am 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
Quote:
cascading delete on one-to-one association


Cascade delete is different from what you described. With cascade delete, deleting the parent causes all the children to be deleted. This is not the same thing is removing the parent reference, thereby breaking the association. That is more like an "all-delete-orphan" which I don't think is supported for one-to-ones.

Quote:
please read the Hibernate documentation before using the word "bug"


Hibernate has been pretty well tested. The "bugs" I find almost always turn out to be user error, in other words, I did something wrong. I think I've only found 2-3 MINOR bugs in over a year and there's always been a work around. Plus, bugs that are found are usually fixed very quickly.

Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 10:30 am 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
I agree that word bug maybe was to strong in this case but definetly it's a missing feature. Because it would be nice to have "transparent persistence".
Without that I need to call explicitlly session.delete(info) ...

Gavin,

Is there is any way how to get initial loaded entity state ? What I mean by that suppose there is entity person I'm calling load person, person state is p1 now I'm calling person.setInfo(null) now I have person state p2. Is there is ahny way how to get person state from the
Code:
SessionImplementator
?
B
ecause then it will be possible to detect orhpaned one-to-one associations and delete them. It would be nice feature to have. Because it allows to have truly transparent one-to-one association management.

Regards,

Giedrius


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 11:01 am 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
What would you call that feature, "reads minds?" It's not cascade delete. Seriously, Hibernate doesn't know if you want the link delete or the link and personInfo deleted.

Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 12:15 pm 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
What are you talking about ?

Ok by default one-to-one assumes that entities (tables) sharing the same primary key so in this case link removal means entity removal.
Or do I need to set primary key on child to null ? :)) So as you can see it's a removal in this case

Of course if you are using property-ref it's a different story then it's a link removal property will be set to null.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 12:24 pm 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
And it's definetly missing feature, and yeah we can call it "read minds (c) jwboring" feature.

It sucks when I need to mix business domain code with call to the session ...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 12:53 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
Quote:
entities (tables) sharing the same primary key


Yea, you've got a point. I have not dealt with one-to-one much lately. There must be some sort of issue with all-delete-orphan with one-to-one's because this works fine for one-to-many.

Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 7:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
trumpc wrote:
Gavin,

Is there is any way how to get initial loaded entity state ? What I mean by that suppose there is entity person I'm calling load person, person state is p1 now I'm calling person.setInfo(null) now I have person state p2. Is there is ahny way how to get person state from the
Code:
SessionImplementator
?
B
ecause then it will be possible to detect orhpaned one-to-one associations and delete them.


No it is not possible. Hibernate supports detached objects, remember? This makes orphan delete for single point associations impossible.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 11:37 am 
Newbie

Joined: Tue Feb 22, 2005 7:33 am
Posts: 3
Gavin, would you be so kind to explain this restriction? Just to put me in position to explain to my co-developers why this is not a "missing feature" :-)

Or even better: make a FAQ out of it.

I'm puzzled especially as "Hibernate in Action" states things different as I quoted in this thread http://forum.hibernate.org/viewtopic.php?t=931506

Many thanks!

_________________
Jonas Kilian


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 11:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Understand the difference between "cascade delete" and "orphan delete", and you will see why one can be implemented and the other not.


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