dstj
dstj

Reputation: 5220

NHibernate JoinQueryOver with a non-visible property throws exception

I'm trying to do this:

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

But I get this exception:

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

I figure it's because the Key object is declared in a way to restrict access to IList and mapped with a non-visible property.

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>();
    ...

And mapped by code as:

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

The question: How can I do it using NHibernate API?

The only way I managed to do it is with 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>();

Upvotes: 1

Views: 531

Answers (1)

Firo
Firo

Reputation: 30803

I'm not firm with mapping by code but something along the lines

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

or Fluent NHibernate (if someones interested)

HasMany(x => x.ConfigValues)
    .Access.CamelCaseField(Prefix.Underscore);

Upvotes: 2

Related Questions