nfplee
nfplee

Reputation: 7977

NHibernate Formula Uses Wrong Prefix

I have the following model:

public class Transaction {
    public virtual decimal Amount { get; set; }
    public virtual decimal Tax { get; set; }
    public virtual decimal TotalAmount { get; set; }
}

The TotalAmount property is mapped as a formula like so:

Map(x => x.TotalAmount).Formula("Amount + Tax");

I then have two derived types (table-per-type):

public class EventTransaction : Transaction {
}

public class ProductTransaction : Transaction {
    public virtual Delivery { get; set; }
}

Now if I do the following query:

var transactions = session.Query<Transaction>().ToList();

The query executes successfully but ideally I'd like the TotalAmount against the product transaction to include the Delivery cost. Therefore in the product mapping class I overwrote the TotalAmount like so:

Map(x => x.TotalAmount).Formula("Amount + Tax + Delivery");

However this didn't work as the generated SQL assumes the Amount and Tax fields are within the product transactions table and prefixes the fields with the wrong alias.

I tried hard coding it with the correct alias from the generated SQL. However whilst this worked for this query, I have found that the alias used is not always the same with alternative queries and subsequently threw an error.

I'd appreciate it if someone could help. Thanks

Upvotes: 1

Views: 274

Answers (1)

gliljas
gliljas

Reputation: 729

Formulas are basically raw SQL statements (which will be processed to add aliases), so you can't access inherited properties etc. But you can write a SQL subselect that gives you what you need.

(SELECT TOP 1 pt.Delivery + t.Tax + t.Amount 
FROM ProductTransaction pt 
INNER JOIN Transaction t ON pt.Id=t.Id 
WHERE pt.Id=Id)

Upvotes: 1

Related Questions