Yes, but instead of returning a object[] or a CustomDTO it would be
nice to be able to return the root entity initialized with only the
fields specified in the query.
for eg.
public class Parent : Entity<Guid>
{
public Parent()
{
Children = new List<Child1>();
}
public virtual string Property1 { get; set; }
public virtual string Property2 { get; set; }
public virtual IList<Child1> Children { get; set; }
}
public class Child1 : Entity<Guid>
{
public Child1()
{
Children = new List<GrandChild>();
}
public virtual string Property1 { get; set; }
public virtual string Property2 { get; set; }
public virtual IList<GrandChild> Children { get; set; }
}
public class GrandChild : Entity<Guid>
{
public virtual string Property1 { get; set; }
public virtual string Property2 { get; set; }
}
Result of the query should be a List of Parent Entities with only
Property1 initialized for Parent, Child and GrandChild
using(var session = sessionFactory.OpenSession())
{
const string hql = @"select p.Property1,
c.Property1, gc.Property1
from Test.Parent as p left join p.Children as c left join c.Children
as gc";
var parents =
session.CreateQuery(hql).SetResultTransformer(Transformers.DistinctRootEntity).List<Parent>();
}
Thanks,
Vikram
On Mar 13, 11:44 am, Oskar Berggren <[email protected]> wrote:
> As for the "select com.id, com.label, com.postCode, com.mayor.name"
> part, yes NHibernate can do that. The return type is a list of
> object[].
>
> Converting that to partly initialised domain objects is not a feature
> of Hibernate, it's a feature of HibernateUtils.find(), which is also
> presented in that blog post. And sounds rather dangerous to me.
>
> /Oskar
>
> 2011/3/13 Vikram Nayak <[email protected]>:
>
> > The following code snippet is from the following blog post:
> >http://www.javalobby.org/articles/hibernate-query-101/
> > for hibernate. I was wondering if there is something similar in
> > Nhibernate.
>
> > --------------------------------------------------------------------------------------------------------------------------------------------------------------
> > Finally, I will present a variation on the previous technique, which
> > avoids the need to create a new data-transfer class for each query.
> > The idea is to retrieve a list of instances of a Hibernate-persisted
> > business class, using a given HQL query, but with only a specified set
> > of columns being instantiated. This allows fast, light-weight queries
> > which return only the minimum necessary information, and avoid complex
> > joins in the HQL queries. For example :
>
> > String query =
> > "select com.id, com.label, com.postCode,
> > com.mayor.name
> > from Community as com
> > left join com.mayor
> > order by com.label ";
>
> > results = HibernateUtils.find(query);
>
> > This query will return a list of Community objects, but with only the
> > 4 specified fields instantiated. Note that this is a powerful
> > technique, as the presentation layer may use ordinary business classes
> > instead of data transfer objects, without having to know the details
> > of the querying techniques being used. T
>
> > --------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> > Thanks,
> > Vikram
>
> > --
> > 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
> > athttp://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.