【发布时间】:2013-10-04 16:58:23
【问题描述】:
我需要使用Criteria 将这个SQL expression翻译成Hibernate
select id,field1,field2,count(*) from my_table
group by field1,field2 having count(*)>1;
这是我目前的代码。
final Session session = ......
ProjectionList projectionList = (ProjectionList)emptyProjection();
projectionList.add(Projections.property("id"),"id");
projectionList.add(Projections.groupProperty("codeI"));
projectionList.add(Projections.groupProperty("codeII"));
projectionList.add(Projections.sqlGroupProjection("count(*)","having count(*)>1",new String[]{},new Type[]{StandardBasicTypes.INTEGER}));
final Criteria criteria = session.createCriteria(MyClass.class).setProjection(projectionList).setResultTransformer(transformer(MyClass.class));
return new ArrayList<MyClass>(criteria.list());
我的问题是
条件正在生成此 SQL。
group by
this_.FIELD1,
this_.FIELD2,
having
count(*)>1
如您所见,第二组 by 后面是 COMMA this_.FIELD2,,而 mySQL 正在抛出 SyntaxError
更新
这是我的hibernate.cfg
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
我们正在使用mysql 5.5.29 和HIBERNATE 4.2.3
希望有人给点建议。
最好的问候..
【问题讨论】:
-
在黑暗中射击 - 您的配置文件中设置了什么 SQL 方言?
-
请查看我编辑的问题,非常感谢。
-
并且可能需要您使用的休眠版本
-
休眠版本 4.2.3
标签: java mysql hibernate criteria