问题描述:

I am trying to SELECT specific fields to fit into mappings I have already setup with the NHibernate. I'm using straight sql queries NOT using HQL. Notice I'm trying to select only specific fields from the action group table. The below works if I put actiongroup.* but I don't want to load all fields because of performance implications on large varchar fields that I don't want to select. I have gotten this to work with linq with NHibernate but in very customized situations I can not use Linq as it has some limitations and complexities that can be avoided with simple SQL. For example in this situation I am left joining and going to get even more complex and linq does not fit the picture. I know you can make a model that has only the fields you want and apply a query that maps perfectly for the fields you want but I'm trying to avoid having to make hundreds of possible DTOs/One off models for every situation. I also want the one to many scenario to work out on these complex queries. My current models already fit the situation but I just need a bit less info in them sometimes. Please remember the code below works but I must change it to actiongroup.* vs picking 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>();

Here is the stack trace and its for field actiongroupname which is in actiongroup. I noticed that NHibernate wants to "Hydrate" all the fields. I was looking for a way for it to skip all other fields (for example) in actiongroup except for what I was wanting to hydrate. The suggestion to use Lazy loading on a property would force me to exclude a lot of fields making this harder and less flexible if I include it in the mapping.

at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)

at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)

at NHibernate.Driver.NHybridDataReader.GetOrdinal(String name) in p:\nhibernate-core\src\NHibernate\Driver\NHybridDataReader.cs:line 363

at NHibernate.Type.NullableType.NullSafeGet(IDataReader rs, String name) in p:\nhibernate-core\src\NHibernate\Type\NullableType.cs:line 236

at NHibernate.Type.NullableType.NullSafeGet(IDataReader rs, String[] names, ISessionImplementor session, Object owner) in p:\nhibernate-core\src\NHibernate\Type\NullableType.cs:line 195

at NHibernate.Type.AbstractType.Hydrate(IDataReader rs, String[] names, ISessionImplementor session, Object owner) in p:\nhibernate-core\src\NHibernate\Type\AbstractType.cs:line 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:line 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:line 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) in p:\nhibernate-core\src\NHibernate\Loader\Loader.cs:line 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:line 876

at NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray, EntityKey optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean returnProxies) in p:\nhibernate-core\src\NHibernate\Loader\Loader.cs:line 342

at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in p:\nhibernate-core\src\NHibernate\Loader\Loader.cs:line 473

at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in p:\nhibernate-core\src\NHibernate\Loader\Loader.cs:line 251

at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in p:\nhibernate-core\src\NHibernate\Loader\Loader.cs:line 1564

From what I see

相关阅读:
Top