NHibernate returning duplicate object in child collections when using Fetch?

You're doing a cartesian product here. Don't. Instead fetch each collection separately.

You're doing a cartesian product here. Don't. Instead, fetch each collection separately.

BTW: this isn't something N the same applies to any ORM in any platform, or even pure SQL without any ORM at all. In general, you don't want to fetch N*M rows when you can fetch N+M instead.

Surely two JOINs (Cartesian product) in a single SQL query is more efficient than 2001 selects, even if it returns more rows (this depends in part on how large the collections are). – schmmd Feb 11 at 6:11 I may have jumped the gun--you are right in most cases.It probably makes sense to issue two queries, one with a single join. – schmmd Feb 11 at 6:31.

You can select single distinct results by specifying SetResultsTransformer(Transformers. DistinctRootEntity).

Thanks. I'll take a look at that. Do you know why this is not the default behaviour?

Who would want the duplicates?! – UpTheCreek Jan 17 at 14:57 This is the same question I asked when I faced your problem. Not a real answer, but let's think what happen in the query side: the resulting recordset will contain duplicates, and NH mimic this background behavior.

In any case NH internally uses the same entity, they are only duplicated entries into the returned list. – Felice Pollano Jan 17 at 15:03 @Felice Pollano, just added the resultstransformer. Unfortunately this did not change anything for me - I'm still getting the duplicates.

Perhaps this is down to the type of collection? – UpTheCreek Jan 17 at 15:10 collection type should not matter. Try to post the code you used for apply the transformer.

– Felice Pollano Jan 17 at 15:15 @Felince - Ok, posted code in answer – UpTheCreek Jan 17 at 15:23.

I am not using N regular 'ol but in the Java version of bernate you can define One-To-Many collections as either Lists or Sets. If you define them as Sets you will not get duplicates. Surprisingly enough, this happens without overriding equals().

Unfortunately, I have the same problem and I want Lists so I can integrate with Wicket but no duplicates...

If your using a bag for example you will get duplicates. You can use a set instead and you won't get duplicates.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions