NHibernate - wanting to get a list with child lists already included

I have several objects:

Public Class Person() { public int Id {get;set;} public IList<Account> Accounts {get;set;} public string Email {get; set;} } public class Account(){ public int Id {get;set;} public IList<AccountPayment> Payments {get;set;} public IList<Venue> Venues {get;set;} } public class AccountPayment(){ public int Id {get;set;} public DateTime PaymentDate {get;set;} public decimal PaymentAmount {get;set;} } public class Venue(){ public int Id {get;set;} public string AddressLine1 {get;set;} public string Postcode {get;set;} } 

These classes are mapped to MS Sql with nHibernate - the table has a table in the class ...

I want to create a method in my repository, GetAccounts (int PersonID), which will return a list with all the child account collections that will be involved in the most efficient way. Can someone give me any instructions on how to do this? I do not want to configure lists as subsamples in my mappings, if I can help ...

Thanks.

+4
source share
2 answers

Well, trying to do this in different ways, I eventually found that the most effective solution for me outlined in this question is:

Unwanted child collection using NHibernate

My question above was a significantly simplified version of the actual call that I had, but using the above method I managed to get db hits to 2 ... a huge improvement from my original implementation.

Thanks for the help and pointers. Learned a little along the way ...

0
source

If you have mapped your classes to tables the way you mention, why don't you just call the Person object to get all your accounts? When you call the Person object from your repository, you can load accounts. For instance:

  public Person GetById(int id) { using (var tx = _sessionBuilder.GetSession().BeginTransaction()) { // -- Lazy load way -- //Person person = _sessionBuilder.GetSession().Get<Person>(id); //tx.Commit(); //return person; // -- Eager load way -- Person person = _sessionBuilder.GetSession().CreateCriteria<Person>() .Add(Restrictions.IdEq(id)) .SetFetchMode("Accounts", FetchMode.Eager) .UniqueResult<Person>(); tx.Commit(); return person; } } 
+1
source

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


All Articles