Hello Sergey,
thanks for having a look at our problem. Its bugging us really hard ;)
--------------------------------------------------------------------------------
This is the database structure:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
This is the object structure:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
These are the XML-Config-Files:
--------------------------------------------------------------------------------
Content of Person.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="property">
<class name="AppCore.Person, AppCore" table="T_PERSON">
<id name="ID" column="ID" access="nosetter.pascalcase-underscore">
<generator class="native" />
</id>
<property name="Name" column="Name" access="nosetter.pascalcase-underscore"/>
<property name="Alter" column="[Alter]" access="nosetter.pascalcase-underscore"/>
<bag fetch="join" name="Adressen" cascade="all" access="nosetter.pascalcase-underscore" lazy="true" inverse="true">
<key column="PersonID" />
<one-to-many class="AppCore.Adresse, AppCore" />
</bag>
</class>
</hibernate-mapping>
Content of Adresse.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="property">
<class name="AppCore.Adresse, AppCore" table="T_ADRESSE">
<id name="ID" column="ID" access="nosetter.pascalcase-underscore">
<generator class="native" />
</id>
<property name="StrasseHausnummer" column="StrHNr" access="nosetter.pascalcase-underscore"/>
<property name="PLZ" column="PLZ" access="nosetter.pascalcase-underscore"/>
<property name="Ort" column="Ort" access="nosetter.pascalcase-underscore"/>
<many-to-one name="Person" column="PersonID" class="AppCore.Person, AppCore" cascade="none" />
</class>
</hibernate-mapping>
--------------------------------------------------------------------------------
This is the code:
--------------------------------------------------------------------------------
Public Sub TestNHibernate()
Dim nhConfig As New NHibernate.Cfg.Configuration
nhConfig.AddClass(GetType(AppCore.Person))
nhConfig.AddClass(GetType(AppCore.Adresse))
Dim SessionFactory As ISessionFactory = nhConfig.BuildSessionFactory()
Dim NSession As ISession = SessionFactory.OpenSession()
' clear Person table
NSession.Delete("FROM Person")
NSession.Flush()
Dim Query As ICriteria = NSession.CreateCriteria(GetType(AppCore.Person)).Add(Expression.Like("Name", "John", NHibernate.Expression.MatchMode.Anywhere))
Dim Personen As IList = Query.List
'If no person exists (because it is deleted above) create some dummy data
If Personen.Count = 0 Then
Dim JWayne As New AppCore.Person
JWayne.Alter = 22
JWayne.Name = "John Wayne"
'Add a first child element
Dim HeimAdresse As New AppCore.Adresse()
HeimAdresse.Person = JWayne
HeimAdresse.StrasseHausnummer = "Burgstr. 4"
HeimAdresse.Ort = "Frankfurt"
HeimAdresse.PLZ = "52134"
JWayne.Adressen.Add(HeimAdresse)
'Add a second child element
Dim FirmenAdresse As New AppCore.Adresse()
FirmenAdresse.Person = JWayne
FirmenAdresse.StrasseHausnummer = "Karl-Friedrich-Str. 74"
FirmenAdresse.Ort = "Cologne"
FirmenAdresse.PLZ = "12345"
JWayne.Adressen.Add(FirmenAdresse)
NSession.Save(JWayne)
NSession.Flush()
End If
Query = NSession.CreateCriteria(GetType(AppCore.Person)).Add(Expression.Like("Name", "Frank", NHibernate.Expression.MatchMode.Start))
'Add a second person
If Query.List.Count = 0 Then
Dim Frank As New AppCore.Person
Frank.Alter = 22
Frank.Name = "Frank Warren"
'Add a first child element
Dim HeimAdresse As New AppCore.Adresse()
HeimAdresse.Person = Frank
HeimAdresse.StrasseHausnummer = "Mittelstra