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: collection mapping
PostPosted: Wed Sep 06, 2006 4:07 pm 
Newbie

Joined: Sat May 07, 2005 11:49 am
Posts: 4
Hi all.

I have a very interesting Problem. My Test App consists of two classes, "Directory" and "Datei" (german for File). The Directory Class contains an IList with holds a collection of Datei objects.

The corresponding Database looks like this:
Image

Finally, I want to get a List of all Directory objects which are in the database, for this, i make a criteria query (see the code below).
As you can see, the Directory Table consists of 2 entries, the Datei Table of 5 entries.

When I now get the List of all Directory objects, I get 5 objects - although i would only expect to get two Directory objects since there are only 2 entries in the database...


Thanks for help...

best regards
markus


Hibernate version:
nhibernate 1.0.2

Mapping documents:

Datei.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="NHibTest.Classes" assembly="NHibTest">
<class name="Datei" table="Datei">
<id name="Id" column="Id" type="Int32">
<generator class="identity" />
</id>
</class>
</hibernate-mapping>

Directory.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="NHibTest.Classes" assembly="NHibTest">
<class name="Directory" table="Directory">
<id name="Id" column="Id" type="Int32">
<generator class="identity" />
</id>
<bag name="Files" lazy="false" fetch="join">
<key column="Directory_FK" />
<one-to-many class="Datei" />
</bag>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

ICriteria crit = session.CreateCriteria(typeof(Directory));
IList data = crit.List();
System.Console.WriteLine(data.Count);


Full stack trace of any exception that occurs:

no exception occurs


Name and version of the database you are using:

MsSql Version 2000


The generated SQL (show_sql=true):

NHibernate: SELECT this.Id as Id1_, files1_.Directory_FK as Director2___, files1_.Id as Id__, files1_.Id as Id0_ FROM Directory this left outer join Datei files1_ on this.Id=files1_.Directory_FK WHERE 1=1


Debug level Hibernate log excerpt:

DEBUG


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 4:53 am 
Newbie

Joined: Wed Sep 06, 2006 9:39 am
Posts: 5
Location: London
In your mapping for directory:
Quote:
<bag name="Files" lazy="false" fetch="join">


What happens if you let NHibernate decide how to fetch i.e. don't command it to use a join?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 6:20 am 
Newbie

Joined: Sat May 07, 2005 11:49 am
Posts: 4
If I let hibernate decide, it makes one select for the first table and then another select for the second table - and this is a little bit inefficient

greets
maecki


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.