Documentation on this is sparse, an many of the blog posts I read say
Entity Projection is not supported in NHibernate. If it's not
supported any suggestions on how to handle this.

I have two simple entities (PRODUCT and PERSON) and a DTO
(PRODUCTDTO). I am trying to project the Product.Id and
Product.InventedBy properties from Product to the ProductDTO which
will be used in a Report.

        public class Product
        {
                public virtual long Id { get; set; }
                public virtual string Name { get; set; }
                public virtual string Category { get; set; }
                public virtual bool Discontinued { get; set; }
                public virtual Person InventedBy { get; set; }
        }

        public class Person
        {
                public virtual long Id { get; set; }
                public virtual string FirstName { get; set; }
                public virtual string LastName { get; set; }
        }

        public class ProductDto
        {
                public long Id { get; set; }
                public Person InventedBy { get; set; }
        }


                        var product = new Product { Name = "Mac",
Category = "Computers" };
                        var person = new Person {FirstName = "Steve", LastName 
= "J"};
                        product.InventedBy = person;
                        IProductRepository repository = new ProductRepository();
                        repository.Add(product);

                       ICriteria criteria =
session.CreateCriteria<Product>();
                       criteria.SetProjection(Projections.Property("Id"));
                       
criteria.SetProjection(Projections.Property("InventedBy"));
        
criteria.SetResultTransformer(Transformers.AliasToBean(typeof(ProductDto)));
                        var list = criteria.List<ProductDto>();
                        foreach (var dto in list)
                        {
                                Console.WriteLine(dto.Id);    //ID is currently 
populated
                                Console.WriteLine(dto.InventedBy.FirstName);  
//InventedBy is not
                        }

-- 
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.

Reply via email to