IndexOutOfRangeException Deep in the bowels of NHibernate - nhibernate

I have the following mappings:
public class SecurityMap : ClassMap<Security>
{
public SecurityMap()
{
Table("Security");
CompositeId().KeyProperty(k => k.Id, "SecurityId").KeyProperty(k => k.EndDate);
Map(x => x.LastUpdateUser);
References(x => x.Company).Columns("CompanyId", "EndDate");
References(x => x.PrimaryListing).Columns("PrimaryListingId", "EndDate");
}
}
public class ListingMap : ClassMap<Listing>
{
public ListingMap()
{
Table("Listing");
CompositeId().KeyProperty(k => k.Id, "ListingID").KeyProperty(k => k.EndDate);
References(x => x.Security).Columns("SecurityId","EndDate");
}
}
public class CompanyMap : ClassMap<Company>
{
public CompanyMap()
{
Table("Company");
CompositeId().KeyProperty(k => k.Id, "CompanyID").KeyProperty(k => k.EndDate);
HasMany(x => x.Securities).KeyColumns.Add("CompanyId", "EndDate");
}
}
When I attempt to run this test:
[Test]
public void can_update_a_security()
{
var repo = IoC.Resolve<ISecurityRepository>();
int someSecurity = 1;
using (var work = IoC.Resolve<IUnitOfWorkManager>().Current)
{
Security security = repo.Get(someSecurity);
security.ShouldNotBeNull();
security.LastUpdateUser = "Dirk Diggler" + DateTime.Now.Ticks;
repo.Save(security);
work.Commit();
}
}
I get the following error deep in the bowels of NHibernate:
Execute
System.IndexOutOfRangeException: Invalid index 6 for this
SqlParameterCollection with Count=6.
at System.Data.SqlClient.SqlParameterCollection.RangeCheck(Int32
index)
at System.Data.SqlClient.SqlParameterCollection.GetParameter(Int32
index)
at System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32
index)
s:\NHibernate\NHibernate\src\NHibernate\Type\DateTimeType.cs(65,0):
at
NHibernate.Type.DateTimeType.Set(IDbCommand
st, Object value, Int32 index)
s:\NHibernate\NHibernate\src\NHibernate\Type\NullableType.cs(180,0):
at
NHibernate.Type.NullableType.NullSafeSet(IDbCommand
cmd, Object value, Int32 index)
s:\NHibernate\NHibernate\src\NHibernate\Type\NullableType.cs(139,0):
at
NHibernate.Type.NullableType.NullSafeSet(IDbCommand
st, Object value, Int32 index,
ISessionImplementor session)
s:\NHibernate\NHibernate\src\NHibernate\Type\ComponentType.cs(213,0):
at
NHibernate.Type.ComponentType.NullSafeSet(IDbCommand
st, Object value, Int32 begin,
ISessionImplementor session)
s:\NHibernate\NHibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs(2393,0):
at
NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object
id, Object[] fields, Object rowId,
Boolean[] includeProperty, Boolean[][]
includeColumns, Int32 table,
IDbCommand statement,
ISessionImplementor session, Int32
index)
s:\NHibernate\NHibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs(2754,0):
at
NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object
id, Object[] fields, Object[]
oldFields, Object rowId, Boolean[]
includeProperty, Int32 j, Object
oldVersion, Object obj, SqlCommandInfo
sql, ISessionImplementor session)
s:\NHibernate\NHibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs(2666,0):
at
NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object
id, Object[] fields, Object[]
oldFields, Object rowId, Boolean[]
includeProperty, Int32 j, Object
oldVersion, Object obj, SqlCommandInfo
sql, ISessionImplementor session)
s:\NHibernate\NHibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs(2940,0):
at
NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object
id, Object[] fields, Int32[]
dirtyFields, Boolean
hasDirtyCollection, Object[]
oldFields, Object oldVersion, Object
obj, Object rowId, ISessionImplementor
session)
s:\NHibernate\NHibernate\src\NHibernate\Action\EntityUpdateAction.cs(78,0):
at
NHibernate.Action.EntityUpdateAction.Execute()
s:\NHibernate\NHibernate\src\NHibernate\Engine\ActionQueue.cs(130,0):
at
NHibernate.Engine.ActionQueue.Execute(IExecutable
executable)
s:\NHibernate\NHibernate\src\NHibernate\Engine\ActionQueue.cs(113,0):
at
NHibernate.Engine.ActionQueue.ExecuteActions(IList
list)
s:\NHibernate\NHibernate\src\NHibernate\Engine\ActionQueue.cs(147,0):
at
NHibernate.Engine.ActionQueue.ExecuteActions()
s:\NHibernate\NHibernate\src\NHibernate\Event\Default\AbstractFlushingEventListener.cs(241,0):
at
NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource
session)
s:\NHibernate\NHibernate\src\NHibernate\Event\Default\DefaultFlushEventListener.cs(19,0):
at
NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent
event)
s:\NHibernate\NHibernate\src\NHibernate\Impl\SessionImpl.cs(1478,0):
at NHibernate.Impl.SessionImpl.Flush()
s:\NHibernate\NHibernate\src\NHibernate\Transaction\AdoTransaction.cs(187,0):
at
NHibernate.Transaction.AdoTransaction.Commit()
at lambda_method(ExecutionScope , ITransaction )
Now the interesting thing is if I comment out the reference to Company or PrimaryListing in the SecurityMap, I don't get the error. It doesn't seem to matter which I comment out. The error only happens when I have both.
When the update actually goes through NHProf shows me this update:
UPDATE Security
SET LastUpdateUser = '2010-02-19T08:09:24.00' /* #p0 */,
CompanyId = 54199 /* #p1 */,
EndDate = '9999-12-31T00:00:00.00' /* #p2 */
WHERE SecurityId = 1 /* #p3 */
AND EndDate = '9999-12-31T00:00:00.00' /* #p4 */
I am not sure why it is updating CompanyId and EndDate, but I suspect it is related.
Any one have ideas? Work arounds would be greatly appreciated.

Yes its a common problem, you are using the Column "EndDate" twice in your mapping definition (for both Company and PrimaryListing) and that is not allowed. One of them has to go, or have an additional EndDate column (one for each association)
check this too
nHibernate 2.0 - mapping a composite-id *and* many-to-one relationship causes "invalid index" error
and
http://devlicio.us/blogs/derik_whittaker/archive/2009/03/19/nhibernate-and-invalid-index-n-for-this-sqlparametercollection-with-count-n-error.aspx

Related

Error when using GetAll (AsyncCrudAppService) and apply sorting

I've built an application service based on the AsyncCrudAppService class, like this:
public class ServiceTemplateAppService : AsyncCrudAppService<ServiceTemplate, ServiceTemplateDto, int, PagedAndSortedResultRequestDto, CreateServiceTemplateDto, UpdateServiceTemplateDto>, IServiceTemplateAppService
Everything works as intended, but once in a while I get an error when calling the GetAll method and passing in a sorting parameter, like this:
api/services/app/CostCenter/GetAll?SkipCount=0&MaxResultCount=10&Sorting=displayName%20asc
I have overridden the CreateFilteredQuery method and it now looks like this because I want to get some child entities.
protected override IQueryable<ServiceTemplate> CreateFilteredQuery(PagedAndSortedResultRequestDto input)
{
return Repository.GetAll()
.Include(x => x.ServiceTemplateRole)
.ThenInclude(y => y.Role)
.Include(x => x.ServiceTemplateImage)
.Include(x => x.ServiceTemplateCategory);
}
The error message that I get is this one:
INFO 2018-11-16 13:45:02,101 [21 ] ore.Mvc.Internal.ControllerActionInvoker - Route matched with {area = "app", action = "GetAll", controller = "ServiceTemplate"}. Executing action dsim.Services.ServiceTemplateAppService.GetAll (dsim.Application)
INFO 2018-11-16 13:45:02,104 [21 ] ore.Mvc.Internal.ControllerActionInvoker - Executing action method dsim.Services.ServiceTemplateAppService.GetAll (dsim.Application) with arguments (Abp.Application.Services.Dto.PagedAndSortedResultRequestDto) - Validation state: Valid
ERROR 2018-11-16 13:45:02,189 [24 ] Mvc.ExceptionHandling.AbpExceptionFilter - Could not load type 'System.ComponentModel.DataAnnotations.BindableTypeAttribute' from assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
System.TypeLoadException: Could not load type 'System.ComponentModel.DataAnnotations.BindableTypeAttribute' from assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
at System.Linq.Dynamic.Core.CustomTypeProviders.AbstractDynamicLinqCustomTypeProvider.<>c.<FindTypesMarkedWithDynamicLinqTypeAttribute>b__0_1(Type x)
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1 other)
at System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection, IEqualityComparer`1 comparer)
at System.Linq.Dynamic.Core.CustomTypeProviders.DefaultDynamicLinqCustomTypeProvider.GetCustomTypes()
at System.Linq.Dynamic.Core.Parser.KeywordsHelper..ctor(ParsingConfig config)
at System.Linq.Dynamic.Core.Parser.ExpressionParser..ctor(ParameterExpression[] parameters, String expression, Object[] values, ParsingConfig parsingConfig)
at System.Linq.Dynamic.Core.DynamicQueryableExtensions.OrderBy(IQueryable source, ParsingConfig config, String ordering, Object[] args)
at System.Linq.Dynamic.Core.DynamicQueryableExtensions.OrderBy[TSource](IQueryable`1 source, ParsingConfig config, String ordering, Object[] args)
at System.Linq.Dynamic.Core.DynamicQueryableExtensions.OrderBy[TSource](IQueryable`1 source, String ordering, Object[] args)
at Castle.Proxies.Invocations.CrudAppServiceBase`6_ApplySorting_3.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Abp.Domain.Uow.UnitOfWorkInterceptor.PerformSyncUow(IInvocation invocation, UnitOfWorkOptions options)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.ServiceTemplateAppServiceProxy.ApplySorting(IQueryable`1 query, PagedAndSortedResultRequestDto input)
at Abp.Application.Services.AsyncCrudAppService`8.GetAll(TGetAllInput input)
at Abp.Threading.InternalAsyncHelper.AwaitTaskWithPostActionAndFinallyAndGetResult[T](Task`1 actualReturnValue, Func`1 postAction, Action`1 finalAction)
at lambda_method(Closure , Object )
at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()
I've tried to figure out what could cause the problem and adding the assembly that the exceptions tells me, but no progress. The strange thing is that this sometimes work, so my guess is that is has something to do with the actual data in the DB, but hey?! I don't know...
GetAll Method From AsyncCrudAppService calls ApplySorting Method, you can override it and there sort as you want.
protected override IQueryable<YourEntity> ApplySorting(IQueryable<YourEntity> query, PagedAndSortedResultRequestDto input)
{
if (input.Sorting.IsNullOrEmpty())
{
input.Sorting = "yourColumn asc";
}
return base.ApplySorting(query, input);
}
If you want to crate another GetAll Method here is an example:
public Task<PagedResultDto<PaisDto>> GetAllFiltered(PagedAndSortedResultRequestDto input, string filter)
{
var paisList = new List<Pais>();
var query = Repository.GetAll();
query = ApplySorting(query, input);
if (filter != null && filter != string.Empty)
{
paisList = query
.Where(x => x.Identificador.StartsWith(filter) || x.Nombre.StartsWith(filter))
.Skip(input.SkipCount)
.Take(input.MaxResultCount).ToList();
var result = new PagedResultDto<PaisDto>(query.Count(), ObjectMapper.Map<List<PaisDto>>(paisList));
return Task.FromResult(result);
}
else
{
paisList = query
.Skip(input.SkipCount)
.Take(input.MaxResultCount).ToList()
.ToList();
var result = new PagedResultDto<PaisDto>(query.Count(), ObjectMapper.Map<List<PaisDto>>(paisList));
return Task.FromResult(result);
}
}
query = ApplySorting(query, input); in this line you can call base method base.ApplySorting(query, input);

TargetInvocationException on Nhibernate QueryOver with JoinAlias

Product productAlias = null;
Session sessionAlias = null;
Slot slotAlias = null;
Price priceAlias = null;
var queryOver = session.QueryOver<Slot>(() => slotAlias)
.JoinAlias(() => slotAlias.Session, () => sessionAlias)
.JoinAlias(() => sessionAlias.Product, () => productAlias);
if (productGuid.HasValue)
{
var productEntity = session.Query<Product>().FirstOrDefault(x => x.Guid == productGuid.Value);
queryOver = queryOver.Where(() => productAlias.Id == productEntity.Id);
}
if (onlyAvailable)
{
queryOver = queryOver.Where(() => slotAlias.StartDate >= DateTimeOffset.UtcNow.AddMinutes(productAlias.Duration));
}
queryOver.List();
When I run this query, I get TargetInvocationException. In the inner message it's a NullReferenceException at slotAlias.StartDate (Line 18, inside onlyAvailable if clause).
Is there something wrong with using aliases with if clauses and multiple Where clauses like this?
StackTrace:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at lambda_method(Closure )
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at NHibernate.Impl.ExpressionProcessor.FindValue(Expression expression)
at NHibernate.Impl.ExpressionProcessor.ProcessSimpleExpression(Expression left, Expression right, ExpressionType nodeType)
at NHibernate.Impl.ExpressionProcessor.ProcessSimpleExpression(BinaryExpression be)
at NHibernate.Impl.ExpressionProcessor.ProcessBinaryExpression(BinaryExpression expression)
at NHibernate.Impl.ExpressionProcessor.ProcessExpression(Expression expression)
at NHibernate.Criterion.QueryOver`2.NHibernate.IQueryOver<TRoot,TSubType>.Where(Expression`1 expression)
at Fullseats.Database.Repositories.Repository.GetSlots(Int32 limit, Int32 offset, Nullable`1 operatorGuid, Nullable`1 productGuid, Nullable`1 from, Nullable`1 to, Boolean onlyAvailable) in C:\Users\erkin\Desktop\FullSeats\Fullseats.Api\Fullseats.Database\Repositories\Repository.cs:line 455
at Fullseats.Server.Core.Services.ProductService.GetSlots(Guid productGuid, PaginationQuery paginationQuery) in C:\Users\erkin\Desktop\FullSeats\Fullseats.Api\Fullseats.Server\Core\Services\ProductService.cs:line 63
at Fullseats.Server.Modules.ProductModule.GetSlotsForProduct(Object arg) in C:\Users\erkin\Desktop\FullSeats\Fullseats.Api\Fullseats.Server\Modules\ProductModule.cs:line 224
at CallSite.Target(Closure , CallSite , Func`2 , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at Nancy.Routing.Route.<>c__DisplayClass4.<Wrap>b__3(Object parameters, CancellationToken context)
With this line:
queryOver.Where(
() => slotAlias.StartDate >= DateTimeOffset.UtcNow.AddMinutes(productAlias.Duration));
You're essentially mixing application-side logic with database-side logic. DateTimeOffset.UtcNow.AddMinutes is executed before the query is sent to the database. That's why passing constant values, but not productAlias.Duration, worked.
Since you're not sending a predefined value as a parameter, you'll need to perform the date manipulation in the database.
Doing this might require a few steps, depending on your dialect. I'm going to assume SQL Server 2012.
Create a custom dialect with a function that defines an addminutes function:
public class MyDialect : MsSql2012Dialect
{
public MyDialect()
{
this.RegisterFunction(
"addminutes",
new SQLFunctionTemplate(NHibernateUtil.DateTimeOffset, "dateadd(n, ?1, ?2)"));
}
}
Use the newly registered function in your query:
queryOver = queryOver.Where(
Restrictions.GeProperty(
Projections.Property(() => slotAlias.StartDate),
Projections.SqlFunction(
"addminutes",
NHibernateUtil.DateTimeOffset,
Projections.Property(() => productAlias.Duration),
Projections.Constant(DateTimeOffset.UtcNow))))
This generates a SQL snippet that looks like this:
WHERE
slotAlia_1.StartDate >= dateadd(minute, this_.Duration, '8/9/2016 2:22:48 AM +00:00');
The C# can be a bit hard to read, you could refactor out the SqlFunction part:
var addMinutesFunction = Projections.SqlFunction(
"addMinutes",
NHibernateUtil.DateTimeOffset,
Projections.Property(() => productAlias.Duration),
Projections.Constant(DateTimeOffset.UtcNow))
queryOver = queryOver.Where(
Restrictions.GeProperty(
Projections.Property(() => slotAlias.StartDate), addMinutesFunction))

How to return IEnumerable <int> in WebAPI (applying filters manually)

I want to return all IDs of my process (Processo class) (apply filters and order before), something like this:
Url: api/processos/getIds?&$filter=(Id eq 1)
public IEnumerable<int> getIds(ODataQueryOptions opts)
{
var results = Repositorio.Query<Processo>();
results = opts.ApplyTo(results) as IQueryable<Processo>;
return results.Select(p => p.Id).ToArray();
}
Error
Full image in: http://i.stack.imgur.com/gzJ7n.jpg
Exception
System.ArgumentException was unhandled by user code
HResult=-2147024809
Message=Cannot apply ODataQueryOptions of 'System.Int32' to IQueryable of 'CreditoImobiliarioBB.Model.Processo'.
Parameter name: query
Source=System.Web.Http.OData
ParamName=query
StackTrace:
at System.Web.Http.OData.Query.ODataQueryOptions`1.ValidateQuery(IQueryable query)
at System.Web.Http.OData.Query.ODataQueryOptions`1.ApplyTo(IQueryable query)
at CreditoImobiliarioBB.Web.Api.processosController.getIds(ODataQueryOptions opts) in w:\Clients\creditoimobiliariobb.com.br\src\CreditoImobiliarioBB\CreditoImobiliarioBB.Web\Api\processosController.cs:line 39
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4()
at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
InnerException:
You need to give ODataQueryOptions a type argument, otherwise it will use the action's return type to build the options. Here is the fixed code:
public IEnumerable<int> getIds(ODataQueryOptions<Processo> opts)
{
var results = Repositorio.Query<Processo>();
results = opts.ApplyTo(results) as IQueryable<Processo>;
return results.Select(p => p.Id).ToArray();
}

Fluent NHibernate Mapping XDocument property to Oracle XMLType

I am looking for the best way to map from an XDocument property type to an Oracle XMLType? I am mapping to a legacy database and have no control over the schema. It is Oracle 9i.
I have read that version 3 of nHibernate provides out the box functionality for this type of mapping. I am using version 3.1 with fluent mappings and I receive the following error when using the default map on a create:
System.ArgumentOutOfRangeException : Specified argument was out of the range of valid values.
at Oracle.DataAccess.Client.OracleParameter.set_DbType(DbType value)
at NHibernate.Driver.DriverBase.SetCommandParameters(IDbCommand cmd, SqlType[] sqlTypes) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Driver\DriverBase.cs: line 180
at NHibernate.Driver.DriverBase.GenerateCommand(CommandType type, SqlString sqlString, SqlType[] parameterTypes) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Driver\DriverBase.cs: line 136
at NHibernate.AdoNet.AbstractBatcher.Generate(CommandType type, SqlString sqlString, SqlType[] parameterTypes) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\AdoNet\AbstractBatcher.cs: line 78
at NHibernate.AdoNet.AbstractBatcher.PrepareBatchCommand(CommandType type, SqlString sql, SqlType[] parameterTypes) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\AdoNet\AbstractBatcher.cs: line 146
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs: line 2616
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPersister.cs: line 3050
at NHibernate.Action.EntityInsertAction.Execute() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Action\EntityInsertAction.cs: line 59
at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs: line 136
at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs: line 125
at NHibernate.Engine.ActionQueue.ExecuteActions() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs: line 170
at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default\AbstractFlushingEventListener.cs: line 241
at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default\DefaultFlushEventListener.cs: line 20
at NHibernate.Impl.SessionImpl.Flush() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs: line 1470
I got round this my writing my own user type which converts between an XDocument and a string:
public SqlType[] SqlTypes
{
get { return (new SqlType[] { new StringClobSqlType() }); }
}
public Type ReturnedType
{
get { return (typeof(XDocument)); }
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
XDocument xDoc = null;
int columnIndex = rs.GetOrdinal(names[0]);
if (!rs.IsDBNull(columnIndex))
{
xDoc = XDocument.Parse((rs[columnIndex].ToString()));
}
return (xDoc);
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
IDbDataParameter parameter = (IDbDataParameter)cmd.Parameters[index];
if (value == null)
{
parameter.Value = DBNull.Value;
}
else
{
XDocument xDoc = (XDocument)value;
parameter.Value = xDoc.ConvertToString();
}
}
This worked fine until the string was greater than 4000 characters in length. Now I get the error:
NHibernate.Exceptions.GenericADOException : could not insert: [XmlBlob#95586][SQL: INSERT INTO XMLBLOB (CAT_CODE, BLB_BLOB, BLB_ID) VALUES (?, ?, ?)]
----> Oracle.DataAccess.Client.OracleException : ORA-01461: can bind a LONG value only for insert into a LONG column
The only update I needed to be able to save more than 4000 characters to the XMLType column was to set the parameter type as OracleDbType.XmlType in the set method for my user type implementation:
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
IDbDataParameter parameter = (IDbDataParameter)cmd.Parameters[index];
((OracleParameter)parameter).OracleDbTypeEx = OracleDbType.XmlType;
if (value == null)
{
parameter.Value = DBNull.Value;
}
else
{
XDocument xDoc = (XDocument)value;
parameter.Value = xDoc.ConvertToString();
}
}

NHibernate dynamic-update fails to update data changed in interceptor

If I set dynamic-update=true I've found that fields updated in my Interceptor do not get included in the update statement that goes to the database. When I set it to false all the columns including the time stamp get updated. I really want to use dynamic update.
public class Interceptor : EmptyInterceptor
{
public override Boolean OnFlushDirty(object entity, object id, object[] state,
object[] previousState, string[] propertyNames, IType[] types)
{
var auditEntity = entity as BaseAuditEntity;
if (auditEntity != null)
{
var now = DateTime.Now;
var index = Array.IndexOf(propertyNames, "LastModifiedTimestamp");
state[index] = now;
auditEntity.LastModifiedTimestamp = now;
}
return base.OnFlushDirty(entity, id, state, previousState, propertyNames, types);
}
}
I thought that this line would have marked my the last modified column as dirty.
auditEntity.LastModifiedTimestamp = now;
Is there something I should do in my interceptor to mark the time stamp field as dirty?
The API-Doc says: "returns true if the user modified the currentState in any way."
Did you try to return true instead of calling the empty base implementation?
public class Interceptor : EmptyInterceptor
{
public override Boolean OnFlushDirty(object entity, object id, object[] state,
object[] previousState, string[] propertyNames, IType[] types)
{
var auditEntity = entity as BaseAuditEntity;
if (auditEntity != null)
{
var now = DateTime.Now;
var index = Array.IndexOf(propertyNames, "LastModifiedTimestamp");
state[index] = now;
auditEntity.LastModifiedTimestamp = now;
return true;
}
return base.OnFlushDirty(entity, id, state, previousState, propertyNames, types);
}
}