Hi, I'm not a Hibernate export, but I don't see how you define the inheritance strategy ?
http://nhforge.org/doc/nh/en/index.html#inheritance-strategies You have to define a strategy of inheritance in your mapping. Regards, Mac. Le mercredi 20 mars 2013 09:25:15 UTC+1, Andre a écrit : > > Hello, > > > I have a class hierarchy that conceptually looks similar to this: > > > <https://lh4.googleusercontent.com/-w3hKntlRKnY/UUlxrFoGCwI/AAAAAAAAAGA/J3kwGDGDT-k/s1600/original.jpg> > > > > That is, there's an abstract base class (Relation) and a couple of derived > classes. In practice, Customer and Supplier share a lot of code, so I > refactored the commonoalities into an abstract class BusinessContact. Now > the actual class hierarchy looks like this: > > > > <https://lh4.googleusercontent.com/-gK8StoiTOLw/UUlxwXsdouI/AAAAAAAAAGI/puqW6tyODqY/s1600/augmented.jpg> > > > Or in code: > > public abstract class Relation > { > public virtual int Id { get; set; } > } > > public class ContactPerson : Relation > { > public virtual string PhoneNumber { get; set; } > } > > public abstract class BusinessContact : Relation > { > public virtual string Name { get; set; } > } > > public class Customer : BusinessContact > { > public virtual string CustomerNumber { get; set; } > } > > public class Supplier : BusinessContact > { > public virtual string SupplierNumber { get; set; } > } > > I'd like to map this hierarchy to four tables (Relation, ContactPerson, > Customer and Supplier) using joined-subclasses in NHibernate, using > mapping-by-code (ModelMapper). My mapping looks like this: > > var mapper = new ModelMapper(); > > mapper.Class<Relation>(map => > { > map.Id(x => x.Id, id => id.Generator(Generators.Native)); > }); > > mapper.JoinedSubclass<ContactPerson>(map => > { > map.Key(key => key.Column("Id")); > map.Property(x => x.PhoneNumber); > }); > > mapper.JoinedSubclass<Customer>(map => > { > map.Key(key => key.Column("Id")); > map.Property(x => x.Name); > map.Property(x => x.CustomerNumber); > }); > > mapper.JoinedSubclass<Supplier>(map => > { > map.Key(key => key.Column("Id")); > map.Property(x => x.Name); > map.Property(x => x.SupplierNumber); > }); > > > > However, as soon as I try to add the mapping to the Configuration I get an > exception: > > NHibernate.MappingException: Cannot extend unmapped class: > BusinessContact > > I basically understand why this happens. The generated mapping looks like > this: > > <?xml version="1.0" encoding="utf-8"?> > <hibernate-mapping xmlns:xsi=" > http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=" > http://www.w3.org/2001/XMLSchema" namespace="JoinedSubClassMapping" > assembly="JoinedSubClassMapping" xmlns="urn:nhib ernate-mapping-2.2"> > <class name="Relation" abstract="true"> > <id name="Id" type="Int32"> > <generator class="native" /> > </id> > </class> > <joined-subclass name="ContactPerson" extends="Relation"> > <key column="Id" /> > <property name="PhoneNumber" /> > </joined-subclass> > <joined-subclass name="Customer" extends="BusinessContact"> > <key column="Id" /> > <property name="CustomerNumber" /> > </joined-subclass> > <joined-subclass name="Supplier" extends="BusinessContact"> > <key column="Id" /> > <property name="SupplierNumber" /> > </joined-subclass> > </hibernate-mapping> > > Customer and Supplier define BusinessContact in their extends attribute, > as if BusinessContact was a "normal" entity in the model. As there's no > mapping for BusinessContact, this fails, or course. Note that the "Name" > property (defined in BusinessContact does not appear in the mapping either. > > What I *want *the mapping to look like is this: > > <?xml version="1.0" encoding="utf-8" ?> > <hibernate-mapping xmlns:xsi=" > http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=" > http://www.w3.org/2001/XMLSchema" namespace="JoinedSubClassMapping" > assembly="JoinedSubClassMapping" xmlns="urn:nhibernate-mapping-2.2"> > <class name="Relation" abstract="true"> > <id name="Id" type="Int32"> > <generator class="native" /> > </id> > </class> > <joined-subclass name="ContactPerson" extends="Relation"> > <key column="Id" /> > <property name="PhoneNumber" /> > </joined-subclass> > <joined-subclass name="Customer" extends="Relation"> > <key column="Id" /> > <property name="CustomerNumber" /> > <property name="Name" /> > </joined-subclass> > <joined-subclass name="Supplier" extends="Relation"> > <key column="Id" /> > <property name="SupplierNumber" /> > <property name="Name" /> > </joined-subclass> > </hibernate-mapping> > > That is, make Supplier and Customer extend Relation and include all the > mapped properties of the (otherwise unmapped) BusinessContact class. > > How can I achieve this? > > Thanks a lot for your time, > Andre > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/nhusers?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
