Anyone using NHibernate here?
I'm using it as the base of our OPF ( Object Persistence Framework ) at the moment.
Although once entity matures a bit more I might swop then again there is supposed to be another version of NHibernate coming out soon.
In life nothing is free - if you haven't paid, someone else has.
I am new to it and loving it. Very powerfull and really enjoying it at the mo, although the learning curve it a little steep. I am having some problems mapping inheritence and was looking for some help.
Have you mapped inheritence at all?
I have a class structure as follows:
I need some help mapping this. I have the following classes:
FinancialTransaction - Abstract class to record all fin transactions in the system
Refund - Subclass derived from Financial Transaction. Represents Refunds
Payment - Abstract class derived from FinancialTransaction. Represents paymentsCCPayment - Subclass derived from Payment. Represents CC PaymentsCashPayment - Subclass derived from Payment. Represents Cash Payments
I am looking at using table per subclass inheritence and am fine with mapping the FinancialTransaction & Refund. I am having problems mapping the Payment and related subclasses.
Mapping attached:
<?xml version="1.0" encoding="utf-8" ?><hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="BlackDiamond.Events.Domain" namespace="BlackDiamond.Events.Domain"> <class name="FinancialTransaction" table="FinancialTransaction" > <id name="Id"> <generator class="guid.comb" /> </id> <property name="TransactionDate" /> <property name="GrossAmount" /> <many-to-one name="RefFinancialTransaction" class="FinancialTransaction" column="RefFinancialTransactionId" cascade="delete"/> <joined-subclass name="Refund" table="Refund"> <key column="Id"/> <property name="RefundReason" column="RefundReason" /> </joined-subclass> </class></hibernate-mapping>
We haven't used the inheritance portion of nHibernate. From what I scanned of the documentation originally, your mapping class makes sense with the join and the relationship but your properties aren't declaring their type in the map. Don't know if this was intentional or not.
About a year ago, we did use inheritance with some of our classes at a very basic level. nHibernate handled the various classes through their respective mapping files even without the specific definitions in the map file for inheritance. Was a while ago though so I may be getting confused with our old OBF of gentle.
Below is the nHibernate documentation for the joined-subclass, in the example they do specify the data type in the subclass element.
Alternatively, a subclass that is persisted to its own table (table-per-subclass mapping strategy) is declared using a <joined-subclass> element.
<joined-subclass name="ClassName" (1) proxy="ProxyInterface" (2) lazy="true|false" (3) dynamic-update="true|false" dynamic-insert="true|false"> <key .... > <property .... /> ..... </joined-subclass>
name: The fully qualified class name of the subclass.
proxy (optional): Specifies a class or interface to use for lazy initializing proxies.
lazy (optional): Setting lazy="true" is a shortcut equalivalent to specifying the name of the class itself as the proxy interface.
No discriminator column is required for this mapping strategy. Each subclass must, however, declare a table column holding the object identifier using the <key> element. The mapping at the start of the chapter would be re-written as:
<?xml version="1.0"?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Eg" namespace="Eg"> <class name="Cat" table="CATS"> <id name="Id" column="uid" type="Int64"> <generator class="hilo"/> </id> <property name="BirthDate" type="Date"/> <property name="Color" not-null="true"/> <property name="Sex" not-null="true"/> <property name="Weight"/> <many-to-one name="Mate"/> <set name="Kittens"> <key column="MOTHER"/> <one-to-many class="Cat"/> </set> <joined-subclass name="DomesticCat" table="DOMESTIC_CATS"> <key column="CAT"/> <property name="Name" type="String"/> </joined-subclass> </class> <class name="Dog"> <!-- mapping for Dog could go here --> </class> </hibernate-mapping>
Thanks for the reply
nhibernate uses reflection to get the data type from the db so you don't actually need to specify the type.
I have gone through the documentation, book and posted on most groups, yet have had no response. Seems most people are also still learning domain driven design and not many c# developers work with inheritance.
Thing is my joined subclass contains further levels of inheritance. Going to keep at it and will post a solution as soon as I have something. This is a scenario that most people should have come across.
Cheers,
S
We mainly use inheritance in our controls and haven't yet had the opportunity to utilize it data wise.
Although, I have a new project on the cards involving a generic "form" ( actual document not IT Form ) in which I want to lay the basic characteristics and have the more detailed aspects ( or specific forms is a better way of saying it) to be child objects inheriting from the generic. Quite keen to see your solution in practice as its pretty much exactly what I would have begun implementing in that case.
Will keep you posted on findings. I have only started using the power of inheritance in my domain model and find it essential with things like
User -> Employee/Member (The same properties exist and an Employee could also be a Member)
Product -> GenericProduct/Specific Product (Generic Product and also very specific products)
The basic scenarios seem to work well with nhibernate, but the real-world is always a little more tricky...
Samuel: The basic scenarios seem to work well with nhibernate, but the real-world is always a little more tricky...
Never a truer word said.