nhibernate / nhibernate-core

NHibernate Object Relational Mapper
https://nhibernate.info
GNU Lesser General Public License v2.1
2.12k stars 924 forks source link

NH-3211 - Allow selecting complex property instead of always projecting every simple property in the entity #825

Closed nhibernate-bot closed 6 years ago

nhibernate-bot commented 6 years ago

Taavi Kõosaar created an issue:

It would be great, if one could do the following with QueryOver, without having to do/maintain projections or use transformers. What i want to get back is the entity.

var st5 = _nHibernateSession.QueryOver<UnitLeadTimeTemplate>()
                       .Where(ultt => ultt.Unit.Id == unitId)
                       .Select(ultt => ultt.LeadTimeTemplate);
st5.List<LeadTimeTemplate>();

This is perhaps not the best business case sample, but it illustrates what i would like to be able to do.

Instead i always have to select all of the properties of LeadTimeTemplate one by one into object[] and then use bean transformer to turn it into the object (and hopefully its not just DTO, but proper persistent entity). Or use the alias approach.

Of course i could just select the UnitLeadTimeTemplate + eager fetch the leadtimtemplate to it and things work nicely, but its with data i do not need. What if i have to join a lot more thing in and filter out and then just need one full entity out of it (its a property of one of them).

bahusoid commented 6 years ago

What i want to get back is the entity

It's a duplicate of already resolved https://github.com/nhibernate/nhibernate-core/issues/948

hazzik commented 6 years ago

@bahusoid can you provide example how to rewrite the query using the new feature? Then close the issue.

bahusoid commented 6 years ago

Since 5.1 you can use "entity projections" to retrieve particular entity from queries:

LeadTimeTemplate ltt = null;
var st5 = _nHibernateSession.QueryOver<UnitLeadTimeTemplate>()
                       .JoinAlias(ultt => ultt.LeadTimeTemplate, () => ltt)
                       .Where(ultt => ultt.Unit.Id == unitId)
                       .Select(x => ltt.AsEntity())

st5.List<LeadTimeTemplate>();

Duplicate of #948 (implemented in https://github.com/nhibernate/nhibernate-core/commit/f2969d0d198efd83c03b5993d0a516ebc0195325)