我正在使用 Play Framework 2.3,当我尝试创建自己的 sql 查询时,总是在忽略映射时遇到问题。我不知道我是否应该使用 Ebean 来编写这个复杂的 sql 查询。这是 SQL 查询运行良好: SELECT g.id, concat(g.client_id,'-',trim(to_char(n.number,'099999'))) as reference, ( SELECT sum(total_ttc) from global_commission_invoice_line where global_commission_invoice_id= g.id ) as totalTTC from global_commission_invoice g LEFT JOIN invoice_number n on g.invoice_number_id = n.id LEFT JOIN client c2 on g.client_id = c2.id我尝试用 ebean 解析它,因为我有一个带参数的WHEN子句。我的 java 代码看起来像。String sql = " SELECT g.id, " + " concat(g.client_id,'-',trim(to_char(n.number,'099999'))) as reference," + " (" + " SELECT sum(total_ttc)" + " from global_commission_invoice_line" + " where global_commission_invoice_id= g.id" + " ) as totalTTC " + " from global_commission_invoice g" + " LEFT JOIN invoice_number n on g.invoice_number_id = n.id" + " LEFT JOIN client c2 on g.client_id = c2.id";// Create sqlRawSql rawSql = RawSqlBuilder.parse(sql) .columnMapping("g.id","id") .columnMappingIgnore("concat(g.client_id,'-',trim(to_char(n.number,'099999')))") .columnMappingIgnore("( SELECT sum(total_ttc) from global_commission_invoice_line where global_commission_invoice_id= g.id )") .create();return Ebean.find(models.GlobalCommissionInvoice.class).setRawSql(rawSql) .findList()此映射忽略成功且没有错误:.columnMappingIgnore("( SELECT sum(total_ttc) from global_commission_invoice_line where global_commission_invoice_id= g.id )")但是这个:.columnMappingIgnore("concat(g.client_id,'-',trim(to_char(n.number,'099999')))")有时我发现错误,大约是一个空格或一个换行符放错了位置,但我从来不知道如何在复杂查询中使用它而不搜索为什么我有几个小时的错误。我应该将 ebean 用于这种类型的 sql 查询吗?我使用映射忽略严重吗?
1 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
我找到了解决办法。我调试 ebean RawSql 类,并使我的映射与 ebean 映射匹配。
Ebean slice SELECT语句在每个逗号处,所以我的concat函数被分成3 部分。
解决方案 :
// Create sql
RawSql rawSql = RawSqlBuilder.parse(sqlStringBuilder.getStrStatement())
.columnMapping("g.id","id")
.columnMappingIgnore("concat(g.client_id")
.columnMappingIgnore("'-'")
.columnMappingIgnore("trim(to_char(n.number")
.columnMappingIgnore("'099999')))")
.columnMappingIgnore("( SELECT sum(total_ttc) from global_commission_invoice_line where global_commission_invoice_id= g.id )")
.create();
添加回答
举报
0/150
提交
取消