Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate mapping to entity using common properties

I am using IQueryable<> to build up batching queries.

I have used views successfully to fetch information so the IQueryable<> can find it, but in this instance I can't work out how to map a view, as it depends on properties rather than the entity's ID.

Say I have this entity and mapping:

public class Calculation 
{
    public virtual int Id { get; set; }
    public virtual Organisation Organisation { get; set; }
    public virtual Charge Charge { get; set; }
    public virtual TransactionTotal TransactionTotal { get; set; }
}

public class CalculationMap : ClassMap<Calculation>
{
    public CalculationMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        References(x => x.Organisation).Not.Nullable().UniqueKey("OC");
        References(x => x.Charge).Not.Nullable().UniqueKey("OC");
    }

This is the class I need to get in there: I'm using a view to give me the total amount per Organisation and Charge:

public class TransactionTotal 
{
    public virtual int Id { get; set; }
    public virtual Organisation Organisation { get; set; }
    public virtual Charge Charge { get; set; }
    public virtual decimal Amount { get; set; }        
}

public class TransactionTotalMap : ClassMap<TransactionTotal>
{
    public TransactionTotalMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        Table("TransactionTotalsView");
        References(x => x.Charge).Not.Nullable();
        References(x => x.Organisation).Not.Nullable();
        Map(x => x.Amount).Precision(15).Scale(2).Not.Nullable();
    }
}

Other places I have used views I have successfully used mappings like HasOne(x => x.TransactionTotal); but in this instance I need to tell Nhibernate to use the Organisation and Charge properties as the key.

Is this even possible? If so, how do I map the TransactionTotal class to the Calculation class?

Edit: I have used CompositeId in TransactionTotalMap as suggested by @David:

CompositeId().KeyProperty(x => x.Organisation.Id).KeyProperty(x => x.Charge.Id);

I'm still stuck on what to put in the CalculationMap though.

like image 717
stuartd Avatar asked Jan 29 '26 13:01

stuartd


1 Answers

use the CompositeId() method in your mapping

like image 128
kͩeͣmͮpͥ ͩ Avatar answered Jan 31 '26 02:01

kͩeͣmͮpͥ ͩ