How to map ICompositeUserType

I am moving a simple working demo from nhibernate to free space. My existing nhibernate mapping is as follows:

<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MoneyHibernate" namespace="MoneyHibernate"> <class name="Invoice" table="Invoices"> <id name="Id"> <generator class="guid"/> </id> <property name="Number"/> <property name="Customer"/> <property name="TotalValue" type="MoneyHibernate.MoneyCompositeUserType,MoneyHibernate"> <column name="TotalValue_Amount" not-null="true" /> <column name="TotalValue_Currency" length="3" not-null="true" /> </property> </class> </hibernate-mapping> 

I tried to create an equlivilant ClassMap :

 internal class InvoiceMap : ClassMap<Invoice> { public InvoiceMap() { Id(x => x.Id); Map(x => x.Customer); Map(x => x.Number); Map(x => x.TotalValue) .CustomType(typeof (MoneyCompositeUserType)) .Column("TotalValue_Amount") .Column("TotalValue_Currency"); } } 

But I get the error:

---> NHibernate.MappingException: the property mapping has the wrong column number: MoneyHibernate.Invoice.TotalValue type: MoneyHibernate.MoneyCompositeUserType

So, I believe that declaring a column twice is not the right way to do this?

+4
source share
1 answer

You are doing it right, however you need to add Columns.Clear() to your collation before manually declaring columns like this:

 Map(x => x.TotalValue) .CustomType(typeof (MoneyCompositeUserType)) .Columns.Clear() .Columns.Add("TotalValue_Amount", "TotalValue_Amount"); 

Otherwise, nHibernate will add new column names in addition to the column collection for matching a composite user type (hence the wrong number of column exceptions).

+6
source

Source: https://habr.com/ru/post/1484588/


All Articles