表
table_supplier_bills - bill_id, supplier_id, date
模型
@Entity
@Table(name = "table_supplier_bills")
public class SupplierBill {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="bill_id")
private Integer billId;
@Column(name="supplier_id")
private Integer supplierId;
@Column(name="date")
private String date;
@Column(name="bill_amount")
@Transient
private BigDecimal billAmount;
@Column(name="paid_amount")
@Transient
private BigDecimal paidAmount;
public SupplierBill() {
super();
}
public Integer getBillId() {
return billId;
}
public void setBillId(Integer billId) {
this.billId = billId;
}
public Integer getSupplierId() {
return supplierId;
}
public void setSupplierId(Integer supplierId) {
this.supplierId = supplierId;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public BigDecimal getBillAmount() {
return billAmount;
}
public void setBillAmount(BigDecimal billAmount) {
this.billAmount = billAmount;
}
public BigDecimal getPaidAmount() {
return paidAmount;
}
public void setPaidAmount(BigDecimal paidAmount) {
this.paidAmount = paidAmount;
}
]
存储库
@Query(value = "SELECT SB.bill_id, SB.date, SB.supplier_id, SUM(SBD.quantity * SBD.rate) as bill_amount, COALESCE((SELECT SUM(SBPD.payment_amount) FROM table_supplier_bill_payment_details SBPD WHERE SBD.bill_id = SBPD.bill_id ),0.00) as paid_amount from table_supplier_bills SB INNER JOIN table_supplier_bill_details SBD ON SB.bill_id = SBD.bill_id WHERE SB.supplier_id = ?1 group by SBD.bill_id ORDER BY SB.bill_id DESC" , nativeQuery = true)
List<SupplierBill> getSupplierBills(Integer sid);
当我调用上述方法时,我没有从服务中获得bill_amount和paid_amount。如果我删除@Transient它可以工作,但后来我无法调用
supplierBillRepository.save(supplierBill);
因为它抱怨未知列bill_amount和paid_amount。
我需要做哪些更改,所以它适用于这两种方法?
答案 0 :(得分:0)
所有非持久字段都应标记为@Transient
,因此save()
仅在您将这些字段标记为@Transient
时才有效。
可以通过定义单独的DTO类(如getSupplierBills()
)来获取SupplierBillResults
方法的聚合字段,如下所示:
public class SupplierBillResults {
private final Date date;
private final Integer billId;
private final Integer supplierId;
private final BigDecimal billAmount;
private final BigDecimal paidAmount;
public SupplierBillResults(Integer billId, Date date,
BigDecimal billAmount, BigDecimal paidAmount, Integer supplierId) {
//set the fields here
}
//Generate getters
}
查询方法:
//Add your @Query here
List< SupplierBillResults> getSupplierBillResults(Integer sid);
注意:您可以使用SupplierBillResults
DTO跨层传输数据(如果需要),并且可以从{{1}中删除@Transient
字段} class,你可以根据你的要求处理它。
EDIT存储库代码
SupplierBill