I've got a difficult little problem here - I'm hoping someone can give me a hint.
I'm trying to create a helper method that allows a caller to pass an entity type and id to have an unloaded object deleted:
Code:
public void Delete(Type entityType, object id)
To actually perform the deletion I'm using code like this:
Code:
session.Delete( String.Format("from {0} entity where entity.id = ?", entityType.Name), id, NHibernateUtil.Object);
The problem is that this doens't work. I get the following exception:
Quote:
Exception: System.IndexOutOfRangeException
Message: Index was outside the bounds of the array.
Source: NHibernate
at NHibernate.Hql.QueryTranslator.PrepareQueryCommand(SqlString sqlString, QueryParameters parameters, Boolean scroll, ISessionImplementor session)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)
at NHibernate.Hql.QueryTranslator.List(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)
NHibernate.ADOException : Could not execute query
----> System.IndexOutOfRangeException : Index was outside the bounds of the array.
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)
at NHibernate.Impl.SessionImpl.Delete(String query, Object[] values, IType[] types)
at NHibernate.Impl.SessionImpl.Delete(String query, Object value, IType type)
at Papa.Framework.Core.Data.Repository.Delete(Type entityType, Object id) in repository.cs:line 311
at Papa.Framework.Core.Services.CustomerServices.DeleteCustomer(Int32 id, ISession session) in customerservices.cs:line 83
at Papa.Framework.Core.Services.CustomerServices.DeleteCustomer(Int32 id) in customerservices.cs:line 75
at Papa.Framework.Core.Tests.Services.CustomerTests.CustomerHighPrivelegeTests.CreateCustomer() in customerhighprivelegetests.cs:line 37
--ADOException
at NHibernate.Hql.QueryTranslator.PrepareQueryCommand(SqlString sqlString, QueryParameters parameters, Boolean scroll, ISessionImplementor session)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)
at NHibernate.Hql.QueryTranslator.List(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)
If I change the query to specify an NHibernate type that matches the type of the Id property then the query will execute as expected. For instance, if the entity's Id property was an Int32 then the following would work:
Code:
session.Delete(String.Format("from {0} entity where entity.id = ?", entityType.Name), id, NHibernateUtil.Int32);
So, is there any way to do this without having to explicitly specify the NHibernate type? Or is there a way to obtain the NHibernate type of the Id property of the entity at runtime?
Cheers,
Symon.