NHibernate projecting child entities into parent properties with Linq or QueryOver

882 views
Skip to first unread message

Thomas JASKULA

unread,
Apr 26, 2011, 5:30:29 PM4/26/11
to nhu...@googlegroups.com

Hello,

 

I’m trying to make a projection with QueryOver. I posted my question on SO because I’m stuck  with it. Here’s the link http://stackoverflow.com/questions/5796036/nhibernate-projecting-child-entities-into-parent-properties-with-linq-or-queryove. Thanks in advance for your help.

 

Thomas

Thomas JASKULA

unread,
Apr 27, 2011, 6:42:32 AM4/27/11
to nhu...@googlegroups.com

Hello,

 

According to the answer http://stackoverflow.com/questions/5796036/nhibernate-projecting-child-entities-into-parent-properties-with-linq-or-queryove?tab=active#tab-top using Criteria I came up with a translation to QueryOver like this :

 

Operation operations = null;

var q = GetSession().QueryOver<User>().Where(u => u.AccessKeyId == accessKeyId)

                                                        .Left.JoinQueryOver(x => x.Operations, () => operations)

                                                        .Select(Projections.ProjectionList()

                                                            .Add(Projections.Sum<User>(x => callWebServicesView. NbSuccessfulAccesses), "TotalSuccessfulAccesses"))

                                                             .Add(Projections.Sum<User>(x => callWebServicesView. NbFailedAccesses), "TotalSuccessfulAccesses"))

                                                            .TransformUsing(Transformers.AliasToBean<UserViewModel>()).List< UserViewModel >();

 

which works great.

 

However it seems that I have to use string to the name of alias "TotalSuccessfulAccesses" and "TotalSuccessfulAccesses" because if I use something like that

 

UserViewModel userView = null;

.Add(Projections.Sum<User>(x => callWebServicesView. NbSuccessfulAccesses),  () => userView.TotalSuccessfulAccesses)

 

NHibernate yields an error :

 

Could not find a setter for property 'userView.TotalSuccessfulAccesses' in class 'Domain.Query.UserViewModel'

 

which is not true because there is a setter for TotalSuccessfulAccesses' property.

 

Any thoughts ?

 

Thank you in advance.

 

Thomas

 

De : Thomas JASKULA [mailto:thoma...@hotmail.fr]
Envoyé : mardi 26 avril 2011 23:30
À : nhu...@googlegroups.com
Objet : NHibernate projecting child entities into parent properties with Linq or QueryOver

Thomas JASKULA

unread,
Apr 27, 2011, 11:06:21 AM4/27/11
to nhu...@googlegroups.com

Hello I tried another approach. FYI I'm using NHibernate 3.1 :

 

Operation operations = null;

UserViewModel uvm = null

var ret = GetSession().QueryOver<User>().Where(u => u.AccessKeyId == accessKeyId)

                                                        .Left.JoinQueryOver(x => x.Operations, () => operations)

                                                        .SelectList(list => list

                                                            .SelectGroup(x => x.Id).WithAlias(() => uvm.Id)

                                                            .SelectSum(x => operations.NbSuccessfulAccesses).WithAlias(() => uvm.TotalSuccessfulAccesses)

                                                             .SelectSum(x => operations.NbFailedAccesses).WithAlias(() => uvm. TotalFailedAccesses))

                                                            .TransformUsing(Transformers.AliasToBean<UserViewModel>()).List< UserViewModel >();

 

That's generate a correct SQL that brings the correct results. Execution of the query works well and NHibernate doesn't yield any errors.

 

However SelectGroup is mapped correctly to the instance 'ret' and SelectSum are not. Am I missing something ? Why the SelectSum are not mapped in the resulting instance ?

 

Thanks for your help.

 

Thomas

 

 

De : nhu...@googlegroups.com [mailto:nhu...@googlegroups.com] De la part de Thomas JASKULA
Envoyé : mercredi 27 avril 2011 12:43
À : nhu...@googlegroups.com
Objet : [nhusers] RE: NHibernate projecting child entities into parent properties with Linq or QueryOver

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

Reply all
Reply to author
Forward
0 new messages