-->
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.  [ 1 post ] 
Author Message
 Post subject: Deleting via query does not work
PostPosted: Thu Sep 01, 2011 7:21 am 
Newbie

Joined: Thu Sep 01, 2011 6:41 am
Posts: 3
Hi there,

I am new to NHibernate and still trying to understand how things work :)
Although I already practised a lot, now I have an unsolvable problem (well - it seems to me as an unsolvable problem).
Now - after all my google skills did not help me - I please you to help me.

Let's go:

I wrote a class "Uebersetzer" (Translator) which inherits from "PersonenDaten" (person-data). Here is the mapping for Uebersetzer (short form):

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="Auftragsdatenbank_Backend"
               namespace="Auftragsdatenbank_Backend">
   <joined-subclass name="Uebersetzer" extends="PersonenDaten">
      <key column="Id"/>

                <property name="UebersetzerNummer" not-null="true"/>

      <set name="Zeitspannen" table="Zeitspannen" cascade="all-delete-orphan">
         <key column="UbersetzerId" />
         <one-to-many class="Zeitspanne"/>
      </set>

   </joined-subclass>
</hibernate-mapping>


FYI: 'Zeitspannen' is another class, which defines two dates (from, until..).

Now i want to delete an "Übersetzer"-object from my database. By doing it that way, everything works fine:

Code:
public void LoescheUebersetzer(Uebersetzer uebersetzer, ISession _session)
        {
  _session.Delete(uebersetzer);
        }


If I call that code, the object of "Zeitspanne" will be deleted also. This is absolutly correct and makes me very happy :)

But now: the problem!
I want to make a query, which deletes a "Übersetzer"-object with a special value, in this case: ÜbersetzerNummer. (Info: "ÜbersetzerNummer is something like TranslatorNumber, and even it is unique, it is set by the user).
Here another code-snipplet:

Code:

public void LoescheUebersetzer(String uebersetzernummer,ISession _session)
{
var hql = @"delete from Uebersetzer u where u.UebersetzerNummer=:unummer";
_session.CreateQuery(hql).SetString("unummer", uebersetzernummer).ExecuteUpdate();
}

This should work, but it doesn't. It seems that NHibernate ignores my cascade settings. Here is the error code:

Quote:
ERROR Could not execute command: DELETE FROM Uebersetzer WHERE (Id) IN (select Id from #Uebersetzer)
System.Data.SqlClient.SqlException: Die DELETE-Anweisung steht in Konflikt mit der REFERENCE-Einschränkung 'FKEAD6ADDC5C21557C'. Der Konflikt trat in der 'auftrag'-Datenbank, Tabelle 'dbo.Zeitspanne', column 'UbersetzerId' auf.
Die Anweisung wurde beendet.


I will try to translate :)
Quote:
ERROR Could not execute command: DELETE FROM Uebersetzer WHERE (Id) IN (select Id from #Uebersetzer)
System.Data.SqlClient.SqlException: the DELETE-statement has a conflict with REFERENCE-restriction 'FKEAD6ADDC5C21557C'. The conflict occured in 'auftrag'-database, table 'dbo.Zeitspanne', column 'UbersetzerId'.
The query has been canceled.


And now the question: why does deleting this way not work AND how can I fix it ?

Thanks a lot for your help and tricks :)

Yours,
DataDummy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.