-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to prevent delete of object with associated objects?
PostPosted: Tue Feb 28, 2006 2:19 am 
Newbie

Joined: Tue Nov 16, 2004 10:18 pm
Posts: 8
Hibernate version:
3.0

Mapping documents:
Code:
<hibernate-mapping>
    <class
        name="com.tougher.intranet.employee.unit.Unit"
        table="Unit">

        <id
            name="unitCode"
            column="unitCode"
            type="java.lang.Long">
            <generator class="native">
            </generator>
        </id>

        <property
            name="unitName"
            type="java.lang.String"
            update="true"
            insert="true"
            column="unitName"/>

        <many-to-one
            name="superUnit"
            class="com.tougher.intranet.employee.unit.Unit"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="superUnitCode"/>

        <set
            name="subUnits"
            lazy="false"
            cascade="none"
            sort="com.tougher.intranet.employee.unit.Unit">
            <key
                column="superUnitCode">
            </key>
            <one-to-many
                  class="com.tougher.intranet.employee.unit.Unit"/>
        </set>
    </class>
</hibernate-mapping>


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

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


I have a recursive relationship... Units associated to superUnits.
When I delete a unit that is a superUnit of other units,
what happens is that the unit is deleted and the subUnit's referenece column becomes null...
Is this is the expected behavior?
What should I change in my mapping.xml file to prevent delete if there are still objects associated to the object in question?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 9:39 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Nothing in your mapping. In your delete method, add "if (!getSubUnits().isEmpty()) { return; }", or equivalent.


Top
 Profile  
 
 Post subject: Re: How to prevent delete of object with associated objects?
PostPosted: Tue Feb 28, 2006 10:37 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
oliverchua wrote:
When I delete a unit that is a superUnit of other units,
what happens is that the unit is deleted and the subUnit's referenece column becomes null...
Is this is the expected behavior?


in the <set ... put cascade="delete-orphan"
will take care of deleting the dangling child records.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 11:03 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
He wants it the other way around: to not delete a unit if it has subunits :)

There is nothing you can do in the mapping to implement this, as it is very non-standard. For one thing, if you issue a delete statement, you expect either a row to be deleted, or an SQL exception to be reported. If you want to conditionally not delete the row, you'll have to implement that in business logic.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 11:25 pm 
Newbie

Joined: Tue Nov 16, 2004 10:18 pm
Posts: 8
thanks tenwit,

you nailed it.
thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 12:46 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It's easier, and more appreciated, to just click the "Yes the post helped" link in the helpful post :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 12:52 am 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
You asked two questions.

Glad to hear tenwits response worked for your second question -- I was answering your first question.

Quote:
Is this is the expected behavior?


Which to elaborate a llittle bit on: Yes what you experienced is expected, and you can use the suggestion that I gave to delete them if you want to.

Hope that helps you...

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 1:18 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
After thinking about the 2nd question a bit more - I realized that have implemented in my testing another solution (with hibernate) that you can do in your mapping.

Try:
<sql-delete callable="true">{? = deleteItem(?)}</sql-delete>

Then in your db-function you can raise an application error if there are children or subordinate objects or any other conditions that violate your business rules - however, I'd like to add one more item. It is probably best to set this type of constraint up in the database itself such that you can't delete a parent that has children - this will also raise a database foreign key exception and also suite your needs.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


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