我正在尝试选择特定字段以适应我已经使用NHibernate设置的映射。我正在使用不使用HQL的直接SQL查询。请注意,我正在尝试仅从操作组表中选择特定字段。下面的工作如果我把actiongroup。*但我不想加载所有字段,因为我不想选择的大型varchar字段的性能影响。我已经使用linq和NHibernate一起工作,但在非常自定义的情况下,我不能使用Linq,因为它有一些限制和复杂性,可以通过简单的SQL避免。例如,在这种情况下,我将离开加入,并将变得更加复杂,而linq不适合图片。我知道你可以创建一个只有你想要的字段的模型,并应用一个完美映射你想要的字段的查询,但我试图避免为每种情况制作数百种可能的DTO / One模型。我还希望一对多方案能够解决这些复杂的查询问题。我目前的模型已经适应了这种情况,但有时我只需要少一些信息。请记住下面的代码有效但我必须将其更改为actiongroup。* vs pick specific fields。
var query = "SELECT appointment.*, actiongroup.actiongroup_id as actiongroup_id,actiongroup.actiongroup_name as actiongroup_name, actiongroupdetail.* from appointment left join actiongroup on appointment_actiongroup_id = actiongroup_id inner join actiongroupdetail on actiongroup_id = actiongroupdetail_actiongroup_id where appointment_id = (:appointment_id)";
var result = _appointmentrepository.SQLQuery(query)
.AddEntity("appointment",typeof(appointment))
.AddJoin("actiongroup", "appointment.actiongroup")
.AddJoin("actiongroupdetail", "actiongroup.actiongroupdetails")
.SetInt32("appointment_id", 1512)//.SetResultTransformer(Transformers.AliasToBean<appointment> ())//.SetResultTransformer(Transformers.AliasToBean<actiongroup>())
.List<appointment>();
这是堆栈跟踪及其在actiongroup中的字段actiongroupname。我注意到NHibernate希望“保湿”所有领域。我正在寻找一种方法让它跳过动作组中的所有其他领域(例如),除了我想要保湿的东西。在属性上使用延迟加载的建议将迫使我排除很多字段,如果我将其包含在映射中,则会使其变得更难且更不灵活。
at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName) at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name) at NHibernate.Driver.NHybridDataReader.GetOrdinal(String name)在p:\ nhibernate-core \ src \ NHibernate \ Driver \ NHybridDataReader.cs:第363行 at NHibernate.Type.NullableType.NullSafeGet(IDataReader rs,String name)在p:\ nhibernate-core \ src \ NHibernate \ Type \ NullableType.cs:第236行 at NHibernate.Type.NullableType.NullSafeGet(IDataReader rs,String [] names,ISessionImplementor session,Object owner)在p:\ nhibernate-core \ src \ NHibernate \ Type \ NullableType.cs:第195行 at NHibernate.Type.AbstractType.Hydrate(IDataReader rs,String [] names,ISessionImplementor session,Object owner)在p:\ nhibernate-core \ src \ NHibernate \ Type \ AbstractType.cs:第131行 at NHibernate.Persister.Entity.AbstractEntityPersister.Hydrate(IDataReader rs,Object id,Object obj,ILoadable rootLoadable,String [] [] suffixedPropertyColumns,Boolean allProperties,ISessionImplementor session)in p:\ nhibernate-core \ src \ NHibernate \ Persister \ Entity \ AbstractEntityPersister.cs:第2518行 at NHibernate.Loader.Loader.LoadFromResultSet(IDataReader rs,Int32 i,Object obj,String instanceClass,EntityKey key,String rowIdAlias,LockMode lockMode,ILoadable rootPersister,ISessionImplementor session)in p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:第989行 at NHibernate.Loader.Loader.InstanceNotYetLoaded(IDataReader dr,Int32 i,ILoadable persister,EntityKey key,LockMode lockMode,String rowIdAlias,EntityKey optionalObjectKey,Object optionalObject,IList hydratedObjects,ISessionImplementor session)在p:\ nhibernate-core \ src \ NHibernate中\ Loader \ Loader.cs:第944行 at NHibernate.Loader.Loader.GetRow(IDataReader rs,ILoadable [] persisters,EntityKey [] keys,Object optionalObject,EntityKey optionalObjectKey,LockMode [] lockModes,IList hydratedObjects,ISessionImplementor session)in p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:第876行 at NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet,ISessionImplementor session,QueryParameters queryParameters,LockMode [] lockModeArray,EntityKey optionalObjectKey,IList hydratedObjects,EntityKey [] keys,Boolean returnProxies)p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:第342行 at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session,QueryParameters queryParameters,Boolean returnProxies)在p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:第473行 at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session,QueryParameters queryParameters,Boolean returnProxies)在p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:第251行 at NHibernate.Loader.Loader.DoList(ISessionImplementor session,QueryParameters queryParameters)在p:\ nhibernate-core \ src \ NHibernate \ Loader \ Loader.cs:第1564行
从我看到的