11

I'm trying to map a value object collection where these contain other value objects but am getting the below exception.

nHibernate Exception:

 ----> NHibernate.PropertyNotFoundException : Could not find a getter for property '_timeAtAddress' in class 'CustomerAddress'

Domain:

public class CustomerAddress
{
    private TimePeriod _timeAtAddress;

    protected CustomerAddress() { }

    public CustomerAddress(TimePeriod timeAtAddress)
    {
        _timeAtAddress = timeAtAddress;
    }

    public TimePeriod TimeAtAddress { get { return _timeAtAddress; } }
}

public class TimePeriod
{
    private readonly int _months;
    private readonly int _years;

    protected TimePeriod() { }

    public TimePeriod(int months, int years)
    {
        _months = months;
        _years = years;
    }

    public int Months { get { return _months; } }
    public int Years { get { return _years; } }
}

nHibernate Mapping:

contact.HasMany<CustomerAddress>(Reveal.Member<Contact>("_customerAddresses"))
    .Schema(...)
    .Table(...)
    .KeyColumn(...)
    .AsBag()
    .Not.LazyLoad()
    .Component(address =>
    {
        .
        .
        .

        address.Component(Reveal.Member<CustomerAddress, TimePeriod>("_timeAtAddress"), timeAtAddress =>
        {
            timeAtAddress.Map(Reveal.Member<TimePeriod>("_years")).Column("TIME_YEARS");
            timeAtAddress.Map(Reveal.Member<TimePeriod>("_months")).Column("TIME_MONTHS");
        });
    });

Had a quick look at Access but can't seem to figure out where to set that up for components. Can you help?

ivan.mylyanyk
  • 2,051
  • 4
  • 30
  • 37
J Fernandes
  • 176
  • 2
  • 7
  • Why are you adding the component in the HasMany relation? Have you tried only call a reference to a Address? – Najera Apr 30 '15 at 18:10
  • Not sure if I follow, what would you change there? – J Fernandes May 01 '15 at 07:58
  • Im asuming you don't need to use the `Component` method in the `HasMany` relation, just guessing you should add only the address reference. Just give it a try. – Najera May 01 '15 at 17:07
  • Wouldn't that force the addresses to be mapped as proper entities instead of value objects? – J Fernandes May 07 '15 at 08:30

2 Answers2

3

Rather than configuring FluentNHibernate to set the private field, shouldn't you be telling it to use the constructor argument?

My gut feeling is that the mistake is here:

address.Component(Reveal.Member<CustomerAddress, TimePeriod>("_timeAtAddress")

Where you're telling it to use the field _timeAtAddress.

Richiban
  • 5,569
  • 3
  • 30
  • 42
1

The only way I managed to move forward (using the private field) was to set a global Access.Field convention.

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Customer>() .Conventions.Add(DefaultAccess.Field()))

J Fernandes
  • 176
  • 2
  • 7