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 nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
On May 28, 2:58 am, Diego Mijelshon <di...@mijelshon.com.ar> wrote:Humm, when is it useful to return multiple Product instances?
> Easy. A query that includes joined child entities returns many copies of the
> parent, as you saw in your query:
> 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)
>
> Now, NHibernate returns an item in the result for each row returned by the
> query, even though both will effectively be the same Product instance,
> thanks to the identity map.
> A better way to do eager collection loading is detailed by Oren here:http://ayende.com/Blog/archive/2010/01/16/eagerly-loading-entity-asso...
From my (newbie) NH user viewpoint, I don't see why it's better. I
mean, I have to be bothered with too much detail "just" for doing a
eager loading. Nonetheless, I will try it. Thanks!
> I believe I mention that article at least 4 times a week, between the listHumm, that seems a sign that maybe there is something odd with the
> and Stackoverflow :-)
default eager loading behavior? It surely triggered my POLA :-(
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.