1

I'm working with the criteria API, querying against a mapping file that I cannot really change. There is a root entity with many child entities joined to it and certain queries have required us to add

criteria.SetResultTransformer(CriteriaSpecification.DistinctRootEntity);

in order to avoid getting duplicate entities when there are multiple SQL result-lines due to the joins. The problem is that now I also want to apply an alias-to-bean transformer, like so:

criteria.SetResultTransformer(Transformers.AliasToBean(typeof(MyDto)));

Using either one of these works great. However, I need to combine them: I want to load only the required columns into a DTO object, and also get only distinct root entities. How can I do this?

1 Answer 1

5

To load only required column into DTO you can use projection with DistinctEntityRootTransformer as follows

ICriteria criteria = session.CreateCriteria(typeof(YourEntity));
criteria.SetProjection(
Projections.Distinct(Projections.ProjectionList()
    .Add(Projections.Alias(Projections.Property("Property"), "Property")));

criteria.SetResultTransformer(
new NHibernate.Transform.AliasToBeanResultTransformer(typeof(MyDto)));

IList list = criteria.List();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.