首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@Calculation注释在OpenXava中不起作用

@Calculation注释在OpenXava中不起作用
EN

Stack Overflow用户
提问于 2021-06-23 00:43:18
回答 1查看 39关注 0票数 0

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

下面是我的@Embeddable的代码,它使用了@Calculation:

代码语言:javascript
复制
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;
}

这是包含可嵌入对象集合的实体的代码:

代码语言:javascript
复制
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不起作用?

EN

回答 1

Stack Overflow用户

发布于 2021-06-24 00:35:34

@Calculation仅在所有操作数都显示在用户界面中时才起作用。在您的示例中,不会显示paymentFrequency.frequencyPerYear,因为paymentFrequency是一个引用,显示为@DescriptionsList。

不用担心,只需使用常规的Java计算属性即可。通过这种方式:

代码语言:javascript
复制
@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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68087628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档