Andy Vie
Andy Vie

Reputation: 11

performance many-to-one relation

I have two tables, with a many-to-one relation and i have a performance problem when reading all data from the database.

ClassA is the parent of ClassB

Configuration ClassA

<id name="Id">
  <generator class="native" />
</id>

<property name="FieldA1" column="FieldA1" />
<many-to-one name="ClassB" column="ClassBId" />

Configuration ClassB

<id name="Id">
  <generator class="native" />
</id>

<property name="FieldB1" column="FieldB1"/>

I would like to a list of classA objects, where the ClassA object contains a classB object at once.

When requesting all data from ClassA, it works very well, but its very slowly because for each entry of ClassA, an sql statement is executed (select * from ClassB where Id==ClassA.ID) to get class B. setting the lazy flag to true, is no solution because i need the data of ClassB.

Anybody an idea how to get NHibernate so that all data can be read with a better performance ?

Thanks !

Upvotes: 0

Views: 340

Answers (2)

DerpDerp
DerpDerp

Reputation: 305

When writing your query, you can also specify the join type, in case not all of your ClassA's have a ClassB.

query.CreateAlias("ClassB", "B", JoinType.LeftOuterJoin);

Upvotes: 0

btilly
btilly

Reputation: 46399

You should be able to SetFetchMode to FetchMode.Join, and then it will send one query to the database. See http://davidhayden.com/blog/dave/archive/2008/12/06/ImprovingNHibernatePerformanceFetchingStrategiesFetchModeFluentNHibernate.aspx for an example that might point you in the right direction.

Upvotes: 1

Related Questions