具有不可见属性的NHibernate JoinQueryOver抛出异常

时间:2011-11-22 19:27:41

标签: nhibernate

我试图这样做:

Key key = session.QueryOver<Key>()
                 .Left.JoinQueryOver<ConfigValue>(x => x.ConfigValues)
                 .Where(c => c.Id == cfgValue.Id)
                 .SingleOrDefault();

但我得到了这个例外:

NHibernate.QueryException was unhandled by user code
  Message=could not resolve property: ConfigValues of: Domain.Model.Key

我认为这是因为Key对象的声明方式是限制对IList的访问并使用不可见的属性进行映射。

public class Key
{
    public virtual int Id { get; protected set; }

    public virtual IEnumerable<ConfigValue> ConfigValues { get { return _configValues; } }
    private IList<ConfigValue> _configValues = new List<ConfigValue>();
    ...

并按代码映射:

Bag<ConfigValue>("_configValues", attr => {
    attr.Lazy(CollectionLazy.Lazy);
    attr.Inverse(false);
    attr.Cascade(Cascade.None);
}, cm => cm.ManyToMany());

问题:我怎样才能使用NHibernate API?

我设法做到的唯一方法是使用HQL:

IList<Key> keys = session.CreateQuery(@"select K_
   from Key as K_
   left outer join K_._configValues as KO_
   where KO_.Id = :cfgValueId ")
        .SetParameter("cfgValueId", cfgValue.Id)
        .List<Key>();

1 个答案:

答案 0 :(得分:2)

我对按代码进行映射不是很坚定,而是按行

Bag<ConfigValue>("ConfigValues", attr => {
    attr.Access("field.camelcase-underscore");
}, cm => cm.ManyToMany());

或流利的NHibernate(如果有人感兴趣的话)

HasMany(x => x.ConfigValues)
    .Access.CamelCaseField(Prefix.Underscore);
相关问题