Designing an NHibernate Entity

I am using NHibernate with the QueryOver API to query my domain objects. The problem is getting duplicate results. For example, when querying the following domain: domain

I use code like:

  var list = Session.QueryOver<Post>()
                    .JoinQueryOver<Comment>(x => x.Comments)
                    .Where(c => c.Name == "Name")
                    .Take(5)
                    .List();

The generated SQL will look like this:

SELECT Top(5) * FROM Posts p left outer join Comments c on p.Id = c.PostId

The problem is that after the left join is completed, as a result, the recordset has more than 5 rows. And then the function is applied TOPand reduces the results. So, for example, if there are 5 comments in the first post, I will receive this post 5 times and do not get others.

, , post . , nhibernate Post Comment select? , - ( JoinQueryOver) ?

+3
3

. , "".

, .

//the alias for post
Post post = null; 

var list = Session.QueryOver(() => post)
            .WithSubquery.WhereProperty(() => post.Id)
                .In(NHibernate.Criterion.QueryOver.Of<Comment>()
                    .Where(c => c.Name == "Name")
                    .Select(c => c.Post.Id))
            .Take(5)
            .List();

SQL - :

SELECT Top(5) * FROM Posts p where p.Id in (select PostID from Comments c where c.Name = 'Name')
+3

, Transformer DistinctRootEntityResultTransformer, , , . , .

0

we used criteria. SetResultTransformer (new DistinctRootEntityResultTransformer ()); to avoid duplicate entries.

-2
source

All Articles