2

I'm using NHibernate version 3.4.0.400 I've tryied to query a nested collection using 2nd Level Cache in the following way:

       _session
           .QueryOver()
           .Left.JoinAlias(x => x.Parent,() => parent)
           .Left.JoinAlias(x => x.Children, () => children)
           .TransformUsing(Transformers.DistinctRootEntity)
           .Cacheable()
           .CacheMode(CacheMode.Normal)
           .Future();

But I'm getting an invalid cast exception:

NHibernate.PropertyAccessException was unhandled by user code
Message=Exception occurred getter of Project.Domain.Entities.EntityBase.Id

 InnerException: System.Reflection.TargetException
   Message=Object does not match target type.
   Source=mscorlib
   StackTrace:
        at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
        at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
        at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
        at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
        at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
        at NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object target)

I've searched on the web and it's said that this bug was fixed already in a earlier version. But it seems that its not.

Some facts:

  • Removing the TransformUsing statement causes the error to disappear.

Similar threads:

  1. First
  2. Second
  3. Third

The entity:

public class Role : EntityBase
{
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual Role Parent { get; set; }
    public virtual IList<Role> Children { get; set; }
}

Mapping:

public class RoleMap : ClassMap<Role>
{
    public RoleMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Description);
        References(x => x.Parent, "ParentId").Nullable();
        HasMany(x => x.Children).Cascade.SaveUpdate().KeyColumn("ParentId");
        Cache.ReadOnly();
    }
}

Configuration:

 var cfg = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2012
                .ConnectionString(connectionString)
                .ShowSql()
                .Driver<Sql2008ClientDriver>()
                .Provider<DriverConnectionProvider>()
                .Dialect<MsSql2008Dialect>())
                .Cache(c => c.ProviderClass<SysCacheProvider>().UseQueryCache().UseSecondLevelCache())
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<EntityBase>().Conventions.Add(DynamicUpdate.AlwaysTrue()))
            .ExposeConfiguration(config =>
            {
                config.SetInterceptor(new SqlStatementInterceptor());
                config.SetProperty(Environment.SqlExceptionConverter, typeof(MsSqlExceptionConverter).AssemblyQualifiedName);
                config.Properties[Environment.CurrentSessionContextClass] = "web";
            }).BuildConfiguration();

If there are any clues please share. Thank you

Community
  • 1
  • 1
Cristian E.
  • 3,116
  • 7
  • 31
  • 61

0 Answers0