0

在我的 OpenXava 应用程序中,@Calculation 注释不起作用。

这是我为使用@Calculation 的@Embeddable 编写的代码:

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;
}

这是具有可嵌入集合的实体的代码:

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;

}

结果如下:

@Calculation 不起作用的集合

请注意,当操作数更改时,不会重新计算最后一列(annualContribution)。

为什么在这种情况下@Calculation 不起作用?

4

1 回答 1

0

@Calculation 仅在所有操作数都显示在用户界面中时才有效。在您的情况下,由于 paymentFrequency 是显示为@DescriptionsList 的参考,因此不会显示 paymentFrequency.frequencyPerYear。

不用担心,只需使用常规的 Java 计算属性即可。这样:

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

在此处了解有关计算属性的更多信息:

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

于 2021-06-23T16:35:33.670 回答