Reputation: 1717
I have a set of payment objects that come from different sources that I'd like to use an TPC inheritance in NHibernate for. The base for all these object is the payment processor (so all have a consistent format), however there is no representation of the base class in the database. I think I have my mapping worked out, except for the fact that I get an exception thrown when trying to insert - "Cannot use identity column key generation with <union-subclass> mapping for: <EntityName>"
Classes:
public class BasePayment
{
public virtual int Id { get; set; }
public virtual PaymentSource Source { get; set; }
public virtual DateTime Date { get; set; }
public virtual decimal Amount { get; set; }
}
public class SubPayment : BasePayment
{
}
Mappings:
public class BasePaymentMap : ClassMap<BasePayment>
{
public BasePaymentMap()
{
UseUnionSubclassForInheritanceMapping();
DiscriminateSubClassesOnColumn("Source");
Id(m => m.Id);
Map(m => m.Source);
Map(m => m.Amount);
Map(m => m.Date);
}
}
public class SubPaymentMap : SubclassMap<SubPayment>
{
public SubPaymentMap()
{
DiscriminatorValue(PaymentSource.SourceX);
Abstract();
Table("SourceXPayments");
}
}
And that's as far as I've got. On a SaveOrUpdate, I'm getting the error above, and a google search is not proving helpful thus far - I have found this question: Cannot use identity column key generation with <union-subclass> ( TABLE_PER_CLASS ) which appears to cover the issue in Java/Hibernate, but I'm having a problem with converting the @GeneratedValue(strategy = GenerationType.TABLE)
into a Fluent syntax, as Fluent NHibernate's GeneratedBy()
method doesn't seem to have a table, or a lot of documentation (that I have found) as to what it does under the covers.
Doing some more reading around it seems that this might not be possible, so if anyone can confirm this or offer a work around for the situation, it'd be greatly appreciated.
If I've not given enough detail, please let me know what would be more use.
Thanks in advance
Upvotes: 2
Views: 3062
Reputation: 30813
UseUnionSubclassForInheritanceMapping();
DiscriminateSubClassesOnColumn("Source");
is conflicting and FNH will ignore one.
UseUnionSubclassForInheritanceMapping
means each class has it's own table with all columns and the table is used to discriminate the class, no column needed
DiscriminateSubClassesOnColumn
means each class of the inheritance hierarchy live in the same table and a column is used to discriminate the classes.
the error message means that when using UseUnionSubclassForInheritanceMapping
identity generation is not allowed since the id's would be unique for one table only but NHibernate threats all subclasses as one set where the id must be unique.
e.g. session.Get<BaseClass>(5);
won't work relyably anymore since there could be more than one subclass with the same id.
@GeneratedValue(strategy = GenerationType.TABLE)
just tells "use another strategy than identity". GeneratedBy.HiLow(); would be a good alternative to identity.
Upvotes: 6