NHibernate二级缓存集合属性缓存未命中

时间:2015-10-28 09:25:01

标签: c# caching nhibernate

我遇到NHibernate和二级缓存问题。 我想做的是以下内容:

  1. 插入实体A(带包)
  2. Retrive Entity A
  3. 我期待的是,当我执行第2步时,实体A和相关包从缓存中检索而不是从数据库中检索。

    我使用集合属性获取缓存未命中(集合中不包含任何项目)。例如:

    我有两个实体:物品和出价,物品有一袋出价

    在我的项目实体中,我有以下属性:

    公共IList Bids {get;组; }

    我得到了以下的缓存缺失(注意Model.Core.Item.Bids)

    Model.Core.Item.Bids#60b8b2d6-4e60-489e-bd08-dce224941b6f

    这是我的NHibernate版本:

    NHibernate 4.0.4 GA NHibernate.Caches.SysCache.dll 4.0.0.4000

    按照Item实体的映射:

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                       assembly="Model.Core"
                       namespace="Model.Core">
    
      <class name="Item" lazy="false">
        <cache include="all" usage="read-write"/>
    
        <id name="Id">
          <generator class="assigned" />
        </id>
    
        <property name="ItemDescription" />
    
        <bag name="Bids" lazy="true" inverse="true" cascade="all" batch-size="1">
          <cache usage="read-write"></cache>
          <key column="idItem"></key>
          <one-to-many class="Bid" />
        </bag>
    
      </class>
    

    关于我对Bid实体的映射:

    </hibernate-mapping>
    
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                       assembly="Model.Core"
                       namespace="Model.Core">
    
      <class name="Bid" lazy="false">
        <cache usage="read-write"/>
    
        <id name="Id" type="Guid">
          <generator class="assigned"/>
        </id>
    
        <property name="BidDescription" />
    
        <many-to-one name="Item" column="idItem" class="Item" lazy ="false"/>
    
      </class>
    
    </hibernate-mapping>
    

    关注我的代码:

    Guid item_guid = Guid.NewGuid();
    
    Guid bid1_guid = Guid.NewGuid();
    Guid bid2_guid = Guid.NewGuid();
    
    using (var session = sessionFactory.OpenSession())
    {
        using (var tx = session.BeginTransaction())
        {
            // item
            Item item = new Item();
            item.Id = item_guid;
            item.ItemDescription = "ItemNote";
    
            // bid1
            Bid bid1 = new Bid();
            bid1.Id = bid1_guid;
            bid1.BidDescription = "Bid1Description";
            bid1.Item = item;
    
            // bid2
            Bid bid2 = new Bid();
            bid2.Id = bid2_guid;
            bid2.BidDescription = "Bid2Description";
            bid2.Item = item;
    
            item.AddBid(bid1);
            item.AddBid(bid2);
    
            session.Save(item);
    
            tx.Commit();
    
    
        }
    }
    
    Item itemX;
    
    using (var session = sessionFactory.OpenSession())
    {
        using (var tx = session.BeginTransaction())
        {
            itemX = session.Get<Item>(item_guid);
            tx.Commit();
        }
    }
    

    这是我的配置:

    var cfg = new Configuration();
    cfg.DataBaseIntegration(db =>
                                {
                                   db.ConnectionString = @"Data Source=.\SQLEXPRESS; Initial Catalog=NHibernateTest; Trusted_Connection=true";
                                   db.Dialect<MsSql2008Dialect>();
                                   db.BatchSize = 500;
                                });
    cfg.AddAssembly("RepositoryNH");
    cfg.SetProperty("hbm2ddl.keywords", "auto-quote");
    
    cfg.SetProperty("cache.use_second_level_cache", "true");
    cfg.SetProperty("cache.default_expiration", "60000");
    cfg.SetProperty("cache.provider_class", "NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache");
    cfg.SetProperty("connection.isolation", "ReadCommitted");
    
    cfg.SetProperty("max_fetch_depth", "10");
    

    非常感谢你 莫里吉奥

0 个答案:

没有答案