0

In my OpenXava application, the @Calculation annotation does not work.

Here my coding for my @Embeddable that uses @Calculation:

import java.math.*;
import java.time.*;
import javax.persistence.*;
import org.openxava.annotations.*;
import lombok.*;

@Getter @Setter
@Embeddable
public class Payment {

    @ManyToOne(fetch=FetchType.EAGER)
    @DescriptionsList
    Paymentfrequency paymentFrequency;

    LocalDate firstPaymentDate;

    @Stereotype("MONEY")
    BigDecimal paymentAmount;

    @ManyToOne(fetch=FetchType.LAZY)
    @DescriptionsList
    Methodofpayment methodOfPayment;

    @ReadOnly
    @Stereotype("MONEY")
    @Calculation("paymentAmount * paymentFrequency.frequencyPerYear")
    BigDecimal annualContribution;
}

And this the code for the entity with the collection of embeddables:

import javax.persistence.*;
import lombok.*;

@Entity @Getter @Setter
public class Paymentfrequency extends GenericType {

    int frequencyPerYear;

    // Payment is used as collection

    @ElementCollection
    @ListProperties("firstPaymentDate, paymentAmount, paymentFrequency,
methodOfPayment, annualContribution")
    Collection<Payment> payments;

}

And this the result:

Collection where @Calculation does not work

Note as the last column (annualContribution) is not recalculated when the operands change.

Why does not work @Calculation in this case?

javierpaniza
  • 677
  • 4
  • 10

1 Answers1

1

@Calculation only works if all the operands are displayed in the user interface. In your case paymentFrequency.frequencyPerYear is not displayed given paymentFrequency is a reference displayed as @DescriptionsList.

Don't worry, just use a regular Java calculated property instead. In this way:

@Stereotype("MONEY")
@Depends("paymentAmount, paymentFrequency.id")
public BigDecimal getAnnualContribution() {
    // You should refine the below code to lead with nulls
    return getPaymentAmount().multiply(getPaymentFrequency().getFrequencyPerYear()); 
}

Learn more about calculated properties here:

https://openxava.org/OpenXavaDoc/docs/basic-business-logic_en.html

javierpaniza
  • 677
  • 4
  • 10