views:

77

answers:

2

I'm using two class NiceCustomer & RoughCustomer which implment the interface ICustomer.

The ICustomer has four properties. They are: 1) Property Id() As Integer 2) Property Name() As String 3) Property IsNiceCustomer() As Boolean 4) ReadOnly Property AddressFullText() As String

I don't know how to map the interface ICustomer, to the database.

I get an error like this in the inner exception. "An association refers to an unmapped class: ICustomer"

I'm using Fluent and NHibernate.

Any help would be greatly appreciated. Thanks in advance.

+1  A: 

It is not possible to map an interface in nhibernate. If your goal is to be able to query using a common type to retrieve both types of customers you can use a polymorphic query. Simply have both your classes implement the interface and map the classes normally. See this reference:

https://www.hibernate.org/hib_docs/nhibernate/html/queryhql.html (section 11.6)

Kevin Stafford
I've tried polymorphic query too but it doesn't work. Still had trouble with that unmapped interface customer. So made it a base class now that works fine. Thanks much Kevin.
Josh
A: 

Hi Josh,

how are you querying? If you're using HQL you need to import the interface's namespace with an HBM file with this line:

<import class="name.space.ICustomer, Customers" />

If you're using Criteria you should just be able to query for ICustomer and it'll return both customer types.

If you're mapping a class that has a customer on it either through a HasMany, HasManyToMany or References then you need to use the generic form:

References<NiceCustomer>(f=>f.Customer)

If you want it to cope with either, you'll need to make them subclasses

Subclassmap<NiceCustomer>

In which case I think you'll need the base class Customer and use that for the generic type parameter in the outer class:

References<Customer>(f=>f.Customer)

Regardless, you shouldn't change your domain model to cope with this, it should still have an ICustomer on the outer class.

I'm not sure if the 1.0RTM has the Generic form working for References but a quick scan of the changes should show the change, which I think is a two line addition.

Stu

related questions