-->
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: many-to-one and delete
PostPosted: Thu Jul 16, 2009 10:39 am 
Newbie

Joined: Tue May 12, 2009 5:54 am
Posts: 9
Hi !
I have a problem.

I have two entities :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping
    xmlns="urn:nhibernate-mapping-2.2"
    default-cascade="none">

    <class
        name="Entites.Structures.Contenu, Core"
        table="CONTENU"
        dynamic-insert="false"
        dynamic-update="false"
        lazy="true">

        <cache usage="read-write" />
        <id name="Id" type="Int64" unsaved-value="null">
            <column name="ID" sql-type="BIGINT"/>
            <generator class="assigned">
            </generator>
        </id>

        ………….

    </class>
</hibernate-mapping>


Code:

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping
    xmlns="urn:nhibernate-mapping-2.2"
    default-cascade="none">

    <class
        name="Entites.Suivis.EtatContenu, Core"
        table="ETAT_CONTENU"
        dynamic-insert="false"
        dynamic-update="false"
        lazy="true">

        <cache usage="read-write" />
        <id name="Id" type="Int64" unsaved-value="0">
            <column name="ID" sql-type="BIGINT"/>
            <generator class="native">
            </generator>
        </id>
……….

        <many-to-one name="Contenu" class="Entites.Structures.Contenu, Core" outer-join="auto" foreign-key="ETAT_CONTENU_CONTENU_FKC">
            <column name="CONTENU_FK" not-null="true" sql-type="BIGINT"/>
        </many-to-one>

    </class>
</hibernate-mapping>


I have two objects :
Code:
moncontenu  (type Contenu)
monetat (type EtatContenu)

monetat.Contenu = moncontenu;


When I delete moncontenu, I get error deleting :
Quote:
ERROR NHibernate.Util.ADOExceptionReporter NHibernate.Util.ADOExceptionReporter - L'instruction DELETE est en conflit avec la contrainte REFERENCE 'ETAT_CONTENU_CONTENU_FKC'. Le conflit s'est produit dans la base de données 'xxxxx', table 'xxxxx.ETAT_CONTENU', column 'CONTENU_FK'.


Any ideas would be greatly appreciated.


Top
 Profile  
 
 Post subject: Re: many-to-one and delete
PostPosted: Mon Jul 20, 2009 5:32 pm 
Newbie

Joined: Mon Jul 20, 2009 5:25 pm
Posts: 4
add

cascade="delete" on your many-to-one or set it to the appropriate value if you want a different type of cascade behavior.


Top
 Profile  
 
 Post subject: Re: many-to-one and delete
PostPosted: Mon Aug 17, 2009 3:46 am 
Newbie

Joined: Tue May 12, 2009 5:54 am
Posts: 9
when i delete one object CONTENU, i want delete objects ETAT_CONTENU.
Your proposition is inverse ???
I dont want delete objects CONTENU when i delele ETAT_CONTENU :).


Top
 Profile  
 
 Post subject: Re: many-to-one and delete
PostPosted: Mon Aug 17, 2009 10:10 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
If you don't have a one-to-many association on contenu, you can't do that with hibernate. But you could do that in the database with "on delete cascade" on the foreign key.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: many-to-one and delete
PostPosted: Mon Aug 17, 2009 10:26 am 
Newbie

Joined: Tue May 12, 2009 5:54 am
Posts: 9
Ok, thanks
if i have a bidirectionnal relatinship between CONTENU and ETAT_CONTENU, i could use cascade delete all.
is it good ?


Top
 Profile  
 
 Post subject: Re: many-to-one and delete
PostPosted: Mon Aug 17, 2009 10:30 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You need "all-delete-orphan" if you want the orphaned children to get deleted.

_________________
--Wolfgang


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.