@Entity
public class Invoice {
InsuranceCompanyInvoiceCalculator insuranceCompanyInvoiceCalculator // how to autowire this?
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
Float ordersTotal;
Float getOrdersTotal() {
insuranceCompanyInvoiceCalculator.getOrdersTotal(this)
}
}
我看过其他类似How to use @Autowired in an class annotated with @Entity?的问题,但它根本无法回答问题。建议创建一个@Service类。但是仍然如何从@Entity类自动连接@Service类?
编辑
我猜想大多数人都建议Entity对象的构造应在您可以访问@Autowire的服务层进行。但在这种情况下不是。
我正在使用Spring Data / Spring Data Rest,在这里您的实体会自动检索并返回到REST调用,而无需编写任何控制器或服务。
在这种情况下,如何从@Entity访问@Autowire
如果要基于通过total
的fields
进行的某种计算来计算Entity
,则可以使用@Formula,也可以查看@Formula hibernate documentation。例如:
@Formula(value = "credit * rate")
private Double total;