禁用为[entity]获取的延迟属性,因为它不支持实体级别的延迟

时间:2013-03-06 14:35:03

标签: c# nhibernate nhibernate-mapping

我正在使用NHibernate:3.2.0和Spring.NET:1.3.2。

用Spring初始化NHibernate后得到一条WARN消息:

  

WARN - 为[Entity]禁用了延迟属性,因为它不支持实体级别的延迟。

我认为这与我的proxy factory(?)有关。 在我的映射中,我在属性级别使用延迟加载:

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="nosetter.camelcase-underscore" assembly="xxx" namespace="xxx">
      <class name="MyClass">
        <id name="Id" column="MY_CLASS_ID">
          <generator class="native">
            <param name="sequence">SQ_MY_CLASS</param>
          </generator>
        </id>
        <version name="Version" />

<!-- omitted -->

        <property name="LoginFailureCount" lazy="true" />

<!-- omitted -->

      </class>
    </hibernate-mapping>

我的代理工厂:

 public class ProxyFactory : NHibernate.Proxy.AbstractProxyFactory
    {

        public override INHibernateProxy GetProxy(object id, ISessionImplementor session)
        {
            INHibernateProxy proxy;
            try
            {
                LazyInitializer advice = new LazyInitializer(EntityName, PersistentClass, id, GetIdentifierMethod, SetIdentifierMethod,
                                                             ComponentIdType, session);
                SerializableProxyFactory factory = new SerializableProxyFactory
                                                   { Interfaces = Interfaces, TargetSource = advice, ProxyTargetType = base.IsClassProxy };
                factory.AddAdvice(advice);
                proxy = (INHibernateProxy)factory.GetProxy();
            }
            catch(Exception exception)
            {
                Log.Error("Creating a proxy instance failed", exception);
                throw new HibernateException("Creating a proxy instance failed", exception);
            }
            return proxy;
        }


        #region Nested type: SerializableProxyFactory

        [Serializable]
        private class SerializableProxyFactory : Spring.Aop.Framework.ProxyFactory
        {
            // Properties
            public override bool IsSerializable
            {
                get { return true; }
            }
        }

        #endregion
    }

当我尝试使用QueryOver(没有映射中的惰性属性)时,它也没有意义:

MyClass myClass = Session.QueryOver<MyClass>() //
                .Fetch(u => u.LoginFailureCount).Lazy
                .SingleOrDefault();

当我查看查询时,NH获取该属性渴望。 当我用调试器观察时,我看到实例myClass没有被代理。 但它不会打印WARN消息。

这是我的类映射到数据库。 LoginFailureCount是一个简单的int

public MyClass
{

    // other properties omitted 

    public virtual int LoginFailureCount 
    {
         get { return _loginFailureCount; }
         set { _loginFauilure = value; }
    }

}

工厂设置在配置中。收集工厂是

     var configuration = new Configuration();
        configuration.Properties.Add(NHibernate.Cfg.Environment.Dialect, nHibernateProperty.NhDialect);
        configuration.Properties.Add(NHibernate.Cfg.Environment.ConnectionProvider, nHibernateProperty.ConnectionProvider);
        configuration.Properties.Add(NHibernate.Cfg.Environment.ConnectionDriver, nHibernateProperty.ConnectionDriverClass);
        configuration.Properties.Add(NHibernate.Cfg.Environment.ConnectionString, nHibernateProperty.GetConnectionString(databaseName));
        configuration.Properties.Add(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, "xxx.ProxyFactoryFactory, [Assebmly]");
        configuration.Properties.Add(NHibernate.Cfg.Environment.CollectionTypeFactoryClass, "xxx.CollectionTypeFactory, [Assembly]");
        configuration.Properties.Add(NHibernate.Cfg.Environment.GenerateStatistics, "false");
        configuration.SetNamingStrategy(new MYNamingStrategy());
        configuration.AddAssembly(Assembly.GetExecutingAssembly());
        configuration.Configure();

我做错了什么?

更新

Spring.NET代理工厂不支持此功能。这就是为什么我不能使用懒惰属性。 NH的内置代理工厂支持延迟属性获取,但有限制,该属性必须是虚拟和自动属性。然后它工作正常。我认为它更像是一种营销功能,而不是一种可以提高性能的东西。

0 个答案:

没有答案