在我的OpenXava应用程序中,@Calculation注释不起作用。
下面是我的@Embeddable的代码,它使用了@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;
}这是包含可嵌入对象集合的实体的代码:
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不起作用?
发布于 2021-06-24 00:35:34
@Calculation仅在所有操作数都显示在用户界面中时才起作用。在您的示例中,不会显示paymentFrequency.frequencyPerYear,因为paymentFrequency是一个引用,显示为@DescriptionsList。
不用担心,只需使用常规的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
https://stackoverflow.com/questions/68087628
复制相似问题