我正在使用 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 sql
RawSql 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 查询吗?我使用映射忽略严重吗?
慕神8447489
相关分类