9
votes

I'm trying to build the MusicStore Nhibernate port, and I've run into this error:

Could not find a getter for property 'OrderInfo' in class 'MvcMusicStore.Models.OrderDetail'

with the following inner exception:

NHibernate.PropertyNotFoundException: Could not find a getter for property 'OrderInfo' in class 'MvcMusicStore.Models.OrderDetail'

while using the following mapping for OrderDetail:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MvcMusicStore" namespace="MvcMusicStore.Models">
  <class name="OrderDetail">
    <id name="Id">
      <generator class="hilo" />
    </id>

    <many-to-one name="OrderInfo" column="OrderInfoId" />
    <property name="Quantity" />
    <property name="UnitPrice" />

    <many-to-one name="Album" column="AlbumId" />
  </class>
</hibernate-mapping>

And the C# class definition:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

    namespace MvcMusicStore.Models
    {
        public class OrderDetail : Entity
        {
            public virtual OrderInfo Order { get; set; }
            public virtual Album Album { get; set; }
            public virtual int Quantity { get; set; }
            public virtual decimal UnitPrice { get; set; }
        }
    }

The mapping for the OrderInfo class is the following:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MvcMusicStore" namespace="MvcMusicStore.Models">
  <class name="OrderInfo">
    <id name="Id">
      <generator class="hilo" />
    </id>

    <property name="OrderDate" />
    <property name="Username" />
    <property name="FirstName" />
    <property name="LastName" />
    <property name="Address" />
    <property name="City" />
    <property name="State" />
    <property name="PostalCode" />
    <property name="Country" />
    <property name="Phone" />
    <property name="Email" />
    <property name="Total" />

    <set name="OrderDetails">
      <key column="OrderInfoId" />
      <one-to-many class="OrderDetail" />
    </set>
  </class>
</hibernate-mapping>

and it has the following C# class definition:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcMusicStore.Models
{
    public class OrderInfo : Entity
    {
        public virtual DateTime OrderDate { get; set; }
        public virtual string Username { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual string Address { get; set; }
        public virtual string City { get; set; }
        public virtual string State { get; set; }
        public virtual string PostalCode { get; set; }
        public virtual string Country { get; set; }
        public virtual string Phone { get; set; }
        public virtual string Email { get; set; }
        public virtual string Total { get; set; }

        public virtual ICollection<OrderDetail> OrderDetails { get; set; }
    }
}

The Entity class is an abstract class which adds an Guid ID property. What am I doing wrong? I honestly can't see any faults with my mappings.

2

2 Answers

6
votes

In the mapping file you say:

<many-to-one name="OrderInfo" column="OrderInfoId" />

and you must say:

<many-to-one name="Order" column="OrderInfoId" />
3
votes

The property name is Order, not OrderInfo. Either rename to property to OrderInfo:

public virtual OrderInfo OrderInfo {get;set;}

or change the config:

<many-to-one name="Order" column="OrderInfoId" />