Java 如何将同一模型中的两个属性相乘,并将其转换为另一个属性
我想在同一个实体中添加两个数字。这是正确的还是怎么做的Java 如何将同一模型中的两个属性相乘,并将其转换为另一个属性,java,spring-boot,model,entity,Java,Spring Boot,Model,Entity,我想在同一个实体中添加两个数字。这是正确的还是怎么做的 @Data @Entity public class Product { @Id private Integer productId; private String productName; private Integer noOfUnits; private Double cartonPrice; @Transient private Double unitPrice=cartonPrice-noOfUnits; } 不,这不是正确的处
@Data
@Entity
public class Product {
@Id
private Integer productId;
private String productName;
private Integer noOfUnits;
private Double cartonPrice;
@Transient
private Double unitPrice=cartonPrice-noOfUnits;
}
不,这不是正确的处理方式!您应该根据需要计算单价字段的控制器在服务类中执行所有这些操作,并将其保存在数据库中
@Data
@Entity
public class Product {
@Id
@Column(name = "product_id")
private Integer productId;
@Column(name = "product_name")
private String productName;
@Column(name = "no_of_units")
private Double noOfUnits;
@Column(name = "carton_price")
private Double cartonPrice;
@Formula("carton_price + no_of_units")
private Double unitPrice;
}
使用此选项可以进行此计算。我不需要将其保存在数据库中。我只需要这个值,然后创建一个名为UnitPriceCalc的类,然后通过创建一个方法在该类中进行单价计算,并将Product对象传递给它,您可以在该方法中设置计算出的单价并返回更新后的对象。无论何时需要单价,都可以使用该类。