【问题标题】:@Calculation annotation does not work in OpenXava@Calculation 注解在 OpenXava 中不起作用
【发布时间】:2021-06-22 16:43:17
【问题描述】:

在我的 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;

}

结果如下:

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

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

【问题讨论】:

    标签: java openxava


    【解决方案1】:

    @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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2023-04-11
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      相关资源
      最近更新 更多