That's the (possible) third step. Second step is determining potential perfomance issues. For example, if your query returns many different publishers, you might run into a SELECT N+1 problem (one SELECT per Publisher after selecting the books). There are several different solutions to that problem. As you mentioned, one is join fetching:
select book from Book book join fetch book.Publisher publisher join fetch publisher.Address address ...etc... But there are other ways to optimize performance, like entity caching and batch-fetching. It all depends on your domain and use cases. By the way, this only applies to the Book entity query. The other one (where I select the _fields_ I'm going to show) does not suffer from those problems (it does suffer from different ones, more design related) Diego On Mon, Jun 28, 2010 at 11:05, Mike <[email protected]> wrote: > Like I mentioned, the example I showed was a mocked up example. I did > not see any optimization of fetch modes in your example. Is that > handled automatically in a query such as that? > > On Jun 24, 7:14 am, Diego Mijelshon <[email protected]> wrote: > > But what you showed was not a complicated query. > > With a correctly defined model, it would start like this: > > > > session.CreateQuery("from Book").List<Book>(); > > > > That is enough to get a list of books, with navigable relationships. > > > > If you'd rather retrieve only the needed fields from the DB for a grid, > you > > could do this: > > > > session.CreateQuery(@" > > select > > ID, > > Name, > > Author.Name, > > Author.Address.State, > > Publisher.Name, > > Publisher.Address.State > > from Book > > ").List(); > > > > You can use a transformer to create a DTO on the fly, but the query is > just > > that. > > > > Diego > > > > > > > > On Thu, Jun 24, 2010 at 00:21, Mike <[email protected]> wrote: > > > I'm up to the last item in your list. That's what I was specifically > > > asking about. I've used NHibernate for simple/intermediate queries > > > but have yet to successfully construct a large query. > > > > > On Jun 23, 5:52 pm, Diego Mijelshon <[email protected]> wrote: > > > > The first problem there is "a Stored Proc that populates a grid". > > > > You've got to change the way you look at this *radically* if you want > to > > > > take advantage of NHibernate. > > > > > > I'll give you some starting steps. > > > > - Create a rich domain model that represents your entities and their > > > > relationships (mapped as references, not Ids) > > > > - Map your domain to the DB. You can use XML, Fluent or ConfORM, > whatever > > > > you like best > > > > - Design your view, with the corresponding data bindings. You can > take > > > two > > > > approaches here: > > > > - Pass the domain objects and bind to nested properties. For > example, > > > > Name, AuthorAddress.State, etc > > > > - Build DTOs/Presentation Models from your domain objects exposing > just > > > > what the grid needs. For example BookName, AuthorAddressState, etc. > > > > - Build a simple query that retrieves the root objects. You can use > HQL, > > > > Criteria, Linq... > > > > - Optimize your query and mappings using joining, batching and > caching to > > > > improve performance as needed > > > > > > Diego > > > > > > On Wed, Jun 23, 2010 at 19:05, Mike <[email protected]> wrote: > > > > > I'm trying to figure out how to do complicated queries in > NHibernate. > > > > > I'm trying to refactor a Stored Proc that populates a grid to an > > > > > NHibernate query, but I'm having problems because it joins a dozen > > > > > tables. I'm aware of setting FetchModes in NHibernate; however, it > > > > > just seems like it's going to be difficult to recreate the results > of > > > > > this query in OO format instead of tabular format. Here's an > example > > > > > query: > > > > > > > SELECT > > > > > Book.ID, > > > > > Book.Name, > > > > > Author.Name, > > > > > AuthorAddress.State, > > > > > Publisher.Name, > > > > > PublisherAddress.State > > > > > FROM > > > > > Book > > > > > INNER JOIN Author ON (Author.ID = Book.AuthorID) > > > > > INNER JOIN Address AuthorAddress ON (AuthorAddress.ID = > > > > > Author.AddressID) > > > > > INNER JOIN Publisher ON (Publisher.ID = Book.PublisherID) > > > > > INNER JOIN Address PublisherAddress ON (PublisherAddress.ID = > > > > > Publisher.AddressID) > > > > > > > Now this is a mocked up example, but you can see the Joins go more > > > > > than one level deep. After I figure in dynamic sorting and paging, > > > > > the Stored Proc yields the exact structure I want to show in my > grid. > > > > > I'm having problems replicating this with NHibernate. Any advice > out > > > > > there? Should I be taking a different approach? I thought about > > > > > keeping a stored procedure and loading it to a simple DTO for > display > > > > > in my grid, but there's domain logic I would love to include in the > > > > > grid, and I'd hate to replicate it. > > > > > > > Thanks! > > > > > > > -- > > > > > You received this message because you are subscribed to the Google > > > Groups > > > > > "nhusers" group. > > > > > To post to this group, send email to [email protected]. > > > > > To unsubscribe from this group, send email to > > > > > [email protected]<nhusers%[email protected]> > <nhusers%[email protected]<nhusers%[email protected]>> > > > <nhusers%[email protected]<nhusers%[email protected]> > <nhusers%252bunsubscr...@googlegroup s.com>> > > > > > . > > > > > For more options, visit this group at > > > > >http://groups.google.com/group/nhusers?hl=en. > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "nhusers" group. > > > To post to this group, send email to [email protected]. > > > To unsubscribe from this group, send email to > > > [email protected]<nhusers%[email protected]> > <nhusers%[email protected]<nhusers%[email protected]>> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/nhusers?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<nhusers%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
