2016-01-19 20 views
0

我在Postgres 9.4.5數據庫的頂部使用Play 2.4.6。Ebean在RawList查詢中綁定的ExpressionList上的附加過濾器中沒有將邏輯名稱轉換爲物理名稱

我想在我的數據庫中獲得捐贈金額的總和,但我希望我的用戶能夠動態過濾查詢中包含的捐贈。我遵循了一些例子,並創建了一個新的類,DonationAggregate,註釋爲@Entity@Sql。由於我想做一個聚合querym我必須手動設置Sql被運行,這工作正常。當我嘗試向連接到我的查詢的ExpressionList中添加一些過濾器時,出現問題 - 我嘗試過濾屬性名稱,但名稱不是從邏輯名稱轉換爲物理名稱。它們被解釋爲邏輯名稱,當然這些列不存在。

我的問題是我如何使用RawSql查詢,還允許使用物理名稱添加ExpressionList上的過濾器?目前,當我運行下面的代碼時,我得到一個異常:Execution exception[[PersistenceException: Query threw SQLException:ERROR: column "lastfour" does not exist

它也可能是值得指出的是,如果我改變 .eq("lastFour", "1234").eq("last_four", "1234") 事情按預期工作。但由於我將應用的過濾器是動態的,並且基於邏輯/屬性名稱生成,所以我無法提前完成這些轉換。

Donation.java

@Entity 
@EntityConcurrencyMode(ConcurrencyMode.NONE) 
public class Donation extends Model { 

    @Id 
    private Long id; 
    private BigDecimal amount; 
    private String lastFour; 

    public List<DonationAggregate> getAggregated() { 

     String rawSql = "select sum(d.amount) as total from donation d"; 

     RawSql rawTransaction = RawSqlBuilder.parse(rawSql).create(); 

     ExpressionList<DonationAggregate> donationFilters = Ebean.find(DonationAggregate.class) 
       .setRawSql(rawTransaction) 
       .where().eq("lastFour", "1234") //---eventually this will be dynamic, so I won't know which columns are being used 
       .findList(); 
    } 

    //---getters/setters 
} 

DonationAggregate.java

@Entity 
@Sql 
public class DonationAggregate { 

    private BigDecimal total; 

    @OneToOne 
    private Donation donation; 

    //---Getters/setters 
} 

回答

0

It also might be worth noting that if I change .eq("lastFour", "1234") to .eq("last_four", "1234")

「lastFour」 不是DonationAggregate的財產,而是捐贈的屬性。這就是爲什麼「lastFour」沒有被翻譯,因爲它不是DonationAggregate的一個已知屬性(它是查詢的根類型)。

嗯。

+0

好點!我嘗試了幾種不同的綁定方式lastFour來捐款,但我無法得到任何東西 'String rawSql =「select sum(d.amount)total total donated d」;' '... eq(「donation。 org.postgresql.util.PSQLException:ERROR:對錶「捐贈」的FROM-clause條目的無效引用提示:也許你的意思是引用表別名「d」。 「 'String rawSql =「select sum(d.amount)as total from donation」;' '.... eq(「donation.lastFour」,「1111」); ' 給我 「org.postgresql.util.PSQLException:錯誤:缺少FROM子句條目表 」d「」 –

+0

最後 '字符串rawSql = 「SELECT SUM(d.amount)從捐贈d」 總;''.... eq(「d.lastFour」,「1111」);' 給我「org.postgresql.util.PSQLException:錯誤:列d.lastfour不存在」 –

+0

where()。eq 「donation.lastFour」,「1111」); ...這是要使用的預期表達式。 –

相關問題