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