Loading...

nhusers@googlegroups.com

[Prev] Thread [Next]  |  [Prev] Date [Next]

[nhusers] Why do I need SetResultTransformer DistinctRootEntity when using FetchMode.Eager? Rui Lopes Thu May 27 17:01:04 2010

Hi,

I'm puzzled about why do I need to call SetResultTransformer on this
snippet:

                var products = session.CreateCriteria<Product>()
                    .SetFetchMode("Descriptions", FetchMode.Eager)
                    // TODO why do I need this?
                    .SetResultTransformer(Transformers.DistinctRootEntity)
                    .List<Product>();

If I don't do that "products" will end up with duplicated Product's
instances.

My test data is as follow. There is one Product tuple, which has two
ProductDescription tuples. something like:

--select * from product
id      price
1       2000.00

--select * from productdescription
id      productId       lang    name
1       1       en      product 1089eefe-ad8e-4835-b817-7d1e37db0574 (en)
2       1       pt      product 1089eefe-ad8e-4835-b817-7d1e37db0574 (pt)

--select * from product as p inner join productdescription as d on
p.id=d.productId
id      price   id      productId       lang    name
1       2000.00 1       1       en      product 
1089eefe-ad8e-4835-b817-7d1e37db0574 (en)
1       2000.00 2       1       pt      product 
1089eefe-ad8e-4835-b817-7d1e37db0574 (pt)


While running the nhibernate query, I would expect it to return just
one Product instance, but it returns two. Why?


I'm using these (fluent) mappings:

        public class ProductDescriptionMap :
ClassMap<ProductDescription>
        {
            public ProductDescriptionMap()
            {
                Id(x => x.Id)
                    .Column("id");
...
                References(x => x.Product)
                    .Not.Nullable()
                    .Column("productId");
            }
        }

        public class ProductMap : ClassMap<Product>
        {
            public ProductMap()
            {
                Id(x => x.Id)
                    .Column("id");
...
                HasMany(x => x.Descriptions)
                  .KeyColumn("productId")
                  .Cascade.All();
            }
        }


What I'm doing wrong?

If this is the expected behavior, can you point me to the rationale
behind it?

Please, see the whole code at http://pastie.org/980393

TIA!

Best regards,
Rui Lopes

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