2016-12-04 47 views
0

如何在春季調用JPA方法時獲得額外的信息4

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和從服務中支付金額。如果我刪除@Transient它的工作原理,但後來我因爲抱怨未知列bill_amount和paid_amount我不能夠調用

supplierBillRepository.save(supplierBill); 

我需要做什麼改變,所以它適用於兩種方法?

+1

你的意思是不持久(標有'@ Transient')這些領域?你很驚訝它沒有在數據存儲中找到它們? –

回答

0

所有非持久性領域應該註明@Transient,所以當你標記這些領域@Transientsave()纔會工作。

getSupplierBills()方法中的聚合字段可以通過定義像SupplierBillResults單獨DTO類中獲取如下圖所示:

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跨層傳輸數據(如果您需要),並且您可以刪除您的您的SupplierBill類的字段,您可以按照您的要求處理此問題。

編輯存儲庫代碼

@Repository 
public interface SupplierBillRepository extends JpaRepository<SupplierBill, Integer> { 
    @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< SupplierBillResults> getSupplierBillResults(Integer sid); 
} 
+0

感謝您的回覆。在做出你所提出的改變後現在我得到這個錯誤 - 沒有找到能夠從類型[java.lang.Integer]轉換爲類型[com.example.model.SupplierBillResults]的轉換器 –

+0

在答案中添加了代碼庫 –