-->
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: help...many-to-one delete when column goes null
PostPosted: Fri Nov 18, 2005 7:28 pm 
Beginner
Beginner

Joined: Thu Nov 03, 2005 4:11 pm
Posts: 25
I have have all kinds of entities in my application which have addresses so i have a separate address table that they all have foreign keys to.

The address mapping is as follows:

Code:
<?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 auto-import="true">
   <class name="Address" table="ADDRESS" >
      <id name="id" column="ID" type="int" unsaved-value="-1" access="field">
         <generator class="identity"/>
      </id>
      <property name="line1" column="LINE1" not-null="true"/>
      <property name="line2" column="LINE2"/>
      <property name="city" column="CITY" not-null="true"/>
      <property name="state" column="STATE" not-null="true"/>
      <property name="postalCode" column="POSTAL_CODE" not-null="true"/>
   </class>
</hibernate-mapping



For instance a person has a work address.


Code:
<?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 auto-import="true">
   <class name="GenericPerson" table="GENERIC_PERSON" >
      <id name="id" column="ID" type="int" unsaved-value="-1" access="field">
         <generator class="identity"/>
      </id>
      <property name="firstName" column="FIRST_NAME"/>
      <property name="lastName" column="LAST_NAME"/>

      <many-to-one name="workAddress" class="com.mbs.model.domainobjects.entities.Address"
      column="WORK_ADDRESS_ID" cascade="all"/>
</class>
</hibernate-mapping>


If i set the workAddress property to null the foreign key to the address table "WORK_ADDRESS_ID" is made null when the session is committed. But the address is not deleted. I would like the address to be deleted when the property is set to null. This seems like a fairly common requirement, so I assume there is an easy way to do this that I must have overlooked.

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 11:53 am 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
Code:
cascade="all-delete-orphan"


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 2:49 pm 
Beginner
Beginner

Joined: Thu Feb 03, 2005 10:42 pm
Posts: 30
Thomas Matzner wrote:
Code:
cascade="all-delete-orphan"


thanks for reply. Oh i tried that. It doesn't work. I think all-delete-orphan is for when you have collections of data. Maybe part of the problem is i have a unidirectional association but it makes sense to me. I have about 10 different entities that all link up to address so having a separate table and just having a unidirectional link is fine.

I know there must be an easy answer, but i can't find it????


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 3:22 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
is there any reason why the Address instance can't be passed to Session.delete() ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 1:23 pm 
Beginner
Beginner

Joined: Thu Nov 03, 2005 4:11 pm
Posts: 25
dennisbyrne wrote:
is there any reason why the Address instance can't be passed to Session.delete() ?


Well i can do a:
session.delete(address);

But I was under the impression that hibernate generally let you manage the child association from the parent. I think it is a lot more intuitive to say
person.setAddress(null). Because generally I will be working from the parent side not the child.

Can I assume from your answer that what I am asking for is not possible?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 1:31 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
afaik, person.setAddress(null) tells hibernate that the Address instance is no longer associated with Person instance. it does not tell hibernate that the address should be deleted from the database. imagine if your use case was that you only wanted to severe the relationship *w/out* deleting the Address ;)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 2:06 pm 
Beginner
Beginner

Joined: Thu Nov 03, 2005 4:11 pm
Posts: 25
dennisbyrne wrote:
afaik, person.setAddress(null) tells hibernate that the Address instance is no longer associated with Person instance. it does not tell hibernate that the address should be deleted from the database. imagine if your use case was that you only wanted to severe the relationship *w/out* deleting the Address ;)


ok i understand. But i have a lot of relationships like this, so would you suggest another way for me to structure my relationships or to approach this problem? I would rather not have the address also have a backlink to person because i have person, user, patient, doctor, facility, etc that all have addresses and i find it advantageous to store the addresses in one address table.

If not I think this would be a very hand feature. Like a on-null attribute where you could set it to delete.


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.