NHibernate Join Fetch (view)

Considering Team โ†’ attitude of the athlete and the request of all athletes. What I don't understand about fetch="Join" ? If this mapping causes the command to load through the connection? When repeating athletes, these are still lazy loads of Team.

 public class AthleteMap : ClassMapping<Athlete> { public AthleteMap() { ManyToOne(a => a.Team, o => { o.Fetch(FetchKind.Join); o.Lazy(LazyRelation.NoLazy); } ); } } 

What this HBM produces:

 <class name="Athlete" table="Athletes"> <id name="Id" type="Int32" /> <property name="FirstName" /> <property name="LastName" /> <many-to-one name="Team" fetch="join" lazy="false" /> <property name="Created" /> </class> 

iteration:

 var session = factory.OpenSession(); foreach (var athlete in session.Query<Athlete>()) Console.WriteLine("{0} {1}", athlete.FirstName, athlete.Team.Name); 
+6
source share
1 answer

NHibernate Linq Query does not use a sampling strategy for matching. You need Fetch () in your linq request like this.

 var session = factory.OpenSession(); foreach (var athlete in session.Query<Athlete>().Fetch(x => x.Team)) Console.WriteLine("{0} {1}", athlete.FirstName, athlete.Team.Name); 

The sampling strategy defined in the mapping document affects:

  • search through Get () or Load ()
  • which happens implicitly when navigating an association.
  • ICriteria Queries
  • HQL queries if subquery fetch is used

source: performance-fetching

+13
source

Source: https://habr.com/ru/post/902148/


All Articles