Note that there are some explanatory texts on larger screens.

plurals
  1. POOnly retrieve specific columns when using Critera queries?
    primarykey
    data
    text
    <p>I'm propositioning NHibernate for a project we're doing at my company and I'd like to know if NHibernate can be optimized to only retrieve specific columns on a table when using the Criteria query language.</p> <p>For example. Let's say I have a table with 30 columns on it and this is mapped to an object using NHibernate that is a 1-for-1 match against the table. However, for a particular function of the system I only care about two of those columns.</p> <p>Now, I know I can use HQL and do a <code>CreateQuery</code> that will accomplish this but that requires I create a constructor for each combination of fields I'd like to selectively retrieve. This could be a huge pain from a maintenance standpoint since I won't catch missing constructors till runtime.</p> <p>I like the Criteria query language since it produces parametrized SQL instead of straight SQL queries from HQL. I see there is an "Exclude" model for not including certain columns but in most cases I will include more columns than exclude.</p> <p>Thanks to the comment below I looked into projections and this still isn't quite the ideal situation for me. When using the following:</p> <pre><code>var list = session .CreateCriteria(typeof (Task)) .SetProjection(Projections .ProjectionList() .Add(Projections.Property("Id"))) .List(); </code></pre> <p>I end up with the variable <code>list</code> just being ints, I'd prefer to have my full Task object but with all the fields set to their default values. Is this even possible? Everything I see so far says no.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload