-->
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.  [ 3 posts ] 
Author Message
 Post subject: Join fetching does not work correct
PostPosted: Thu Oct 13, 2005 3:43 am 
Newbie

Joined: Thu Oct 13, 2005 3:40 am
Posts: 3
Hello!

I am trying to fix the following problem for hours now, so maybe you can help me getting it work:

All I want to do is to load some data from two joined tables into objects. Searching the web lead me to the code below, but it does not work correctly! There

are no compilation errors, but the number af children is never correct.
Code:
   <class name="Parent,Core" table="T_Parent" dynamic-update="true">
      <id name="ParentID" column="ParentID" type="Int32">
         <generator class="identity" />
      </id>

      <property name="some columns...
      .
      .
      .

      <set name="Children" fetch="join" cascade="all" access="nosetter.camelcase-underscore" lazy="false" inverse="true">
         <key column="ParentId" />
         <one-to-many class="Children, Core" />
      </set>
   </class>


   <class name="Children,Core" table="T_Children">
      <composite-id>
         <key-property name="Name" column="Name"/>
         <key-property name="LastName" column="LastName"/>
         <key-property name="Age" column="Age"/>
         <key-property name="ParentID" column="ParentID"/>
         
      </composite-id>
      
      <property name="some columns...
      .
      .
      .
   </class>


The goal is to access the children objects by parent.children(number).

What happens is that NHibernate creates an outer join which leads to dublicated primary key entries in my parent object list and thus a wrong number of

childs of every parent entry.

Perhaps this is easier to understand the problem:
If I use a bag than the outer join deliveres one row per child, also including the parent-id. But NHibernate does not map this correctly to my objects.

If I use a set it returns the correct number of parents but a too small number of childs.

Another thing I have tried was to use an inverse end by defining a <key-to-many... in the children class which causes a stack overflow when using
fetch="join" and works while using fetch="select".


By the way: What is the main difference between bag and set?

Thanks for any advice.

Regards
Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 11:53 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
How are you actually loading the objects? Can you please post the code?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 4:51 am 
Newbie

Joined: Thu Oct 13, 2005 3:40 am
Posts: 3
Hello Sergey,

thanks for having a look at our problem. Its bugging us really hard ;)

--------------------------------------------------------------------------------
This is the database structure:
--------------------------------------------------------------------------------
Image
--------------------------------------------------------------------------------
This is the object structure:
--------------------------------------------------------------------------------
Image

--------------------------------------------------------------------------------
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


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