开发者

could not get a field value by reflection getter... nhibernate query cache with 2nd level cache

开发者 https://www.devze.com 2023-03-13 21:19 出处:网络
I got this exception could not get a field value by reflection getter of DictionaryMaster.k__BackingField

I got this exception

could not get a field value by reflection getter of DictionaryMaster.k__BackingField

with this inner exception:

Field '<>k__BackingField' defined on type is not a field on the target object which is of type 'System.Object[]'.

The problem exists only when i use eagerloading in query. Below i put my classes, relations and query.

public class DictionaryMaster
    {
        public virtual IList<DictionaryItem> DictionaryItems { get; private set; }
        public virtual System.String Code { get; private set; }
        public virtual System.String Description { get; private set; }
    }

   public class DictionaryMasterMap : ClassMap<DictionaryMaster>
    {
        public DictionaryMasterMap()
        {
            Cache.ReadOnly().Region("dictionary");
            LazyLoad();

            Id(x => x.Code) //i know this is so ugly
                .Column("DC_Code")
                .GeneratedBy.Assigned(); 
            Map(x => x.Description).Column("DC_Desc");
            HasMany(x => x.DictionaryItems)
                .Cascade.AllDeleteOrphan()
                .Fetch.Select()
                .AsBag()
                .Inverse()
                .Not.LazyLoad()
                .KeyColumns.Add("DI_DCCode");
        }
    }

 public class DictionaryItem
    {
        public virtual int Id { get; private set; }
        public virtual string Code { get; private set; }
        public virtual DictionaryMaster DictionaryMaster { get; private set; }
        public virtual string Description { get; private set; }
}

   public class DictionaryItemMap : Clas开发者_JAVA技巧sMap<DictionaryItem>
    {
        public DictionaryItemMap()
        {
            Cache.ReadOnly().Region("dictionary");

            Id(x => x.Id)
                .Column("DI_Id").GeneratedBy.Identity();

            Map(x => x.Code).Column("DI_Code");
            Map(x => x.Description).Column("DI_Desc");
            References(x => x.DictionaryMaster).Column("DI_DCCode");
        }
    }

Query:

session.Query<DictionaryMaster>()
                    .Fetch(x => x.DictionaryItems)
                    .Cacheable()
                    .CacheMode(CacheMode.Normal)
                    .ToList();


I suspect many users are facing this problem - perhaps if you unmark your answer as the chosen answer the question will get more attention. AFAIK there's still no workaround which allows using Linq, Cacheable() and Fetch() at the same call.

This is meant as a comment, however probably because of my low SO ranking I can't create comments yet.

Cheers,

Jonno


I found what is wrong:

First: I really don't know why Fluent NHibernate maps my Id using FieldBacking, because I have property access.

Second: When I removed private modifier for the setter then it showed this exception:

Exception occurred getter of xxx'

The exception brought me to this page https://nhibernate.jira.com/browse/NH-2587. And now I am wondering about some workarounds. Any ideas?

0

精彩评论

暂无评论...
验证码 换一张
取 消