我在Java Hibernate的查询中使用了WHEN CASE条件。在MySQL工作台中查询工作正常,但在Java控制台中给我这个例外。
:org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found 'WHEN' near line 1.
这是我的查询,
StringBuffer hql = new StringBuffer();
hql.append(" SELECT ");
hql.append(" v.year,");
hql.append(" v.number,");
hql.append(" ap.property,");
hql.append(" CASE");
hql.append(" WHEN ap.id = 1 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 2 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 3 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 4 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 5 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 6 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 7 THEN AVG(v.Month101)");
hql.append(" WHEN ap.id = 8 THEN AVG(v.Month101)");
hql.append(" WHEN ap.id = 9 THEN AVG(v.Month101)");
hql.append(" WHEN ap.id = 10 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 11 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 12 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 13 THEN AVG(v.Month101)");
hql.append(" WHEN ap.id = 14 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 15 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 16 THEN AVG(v.Month101)");
hql.append(" WHEN ap.id = 17 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 18 THEN AVG(v.Month101)");
hql.append(" WHEN ap.id = 19 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 20 THEN AVG(v.Month101)");
hql.append(" WHEN ap.id = 21 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 22 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 23 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 24 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 25 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 26 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 27 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 28 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 29 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 30 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 31 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 32 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 33 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 34 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 35 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 36 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 37 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 38 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 39 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 40 THEN SUM(v.Month101)");
hql.append(" WHEN ap.id = 41 THEN SUM(v.Month101)");
hql.append(" END AS jan,");
hql.append(" FROM");
hql.append(" table1 v,");
hql.append(" table2 ap");
hql.append(" WHERE");
hql.append(" v.number=01");
hql.append(" AND v.Id = ap.id");
hql.append(" AND v.year =2015");
hql.append(" GROUP BY ap.property");
hql.append(" ORDER BY v.Id");
我有这个条件12个月,语法有什么问题?可以解释一下吗?
答案 0 :(得分:0)
我认为由于 END AS jan
之后的逗号逗号导致语法错误hql.append(" END AS jan,");
hql.append(" FROM");
在上面的 jan 之后删除逗号并再次检查 - 我想它会起作用。
答案 1 :(得分:0)
发现语法错误。错过了一个结束括号SUM(v.Month101“);
感谢您的建议。