Sql server MSSQL问题休眠标准
我将JavaHibernate与两个数据库(Postgresql和MSSQL)一起使用。 带方言的SqlServer2012:Sql server MSSQL问题休眠标准,sql-server,hibernate,hibernate-criteria,Sql Server,Hibernate,Hibernate Criteria,我将JavaHibernate与两个数据库(Postgresql和MSSQL)一起使用。 带方言的SqlServer2012: hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect 我编写了一个条件查询,如: DetachedCriteria detachedCriteria=DetachedCriteria.forClass(entityClazz); ProjectionList proj = Projections.
hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect
我编写了一个条件查询,如:
DetachedCriteria detachedCriteria=DetachedCriteria.forClass(entityClazz);
ProjectionList proj = Projections.projectionList();
proj.add(Projections.max(COMPOSEDID_VERSION_ID));
proj.add(Projections.groupProperty(COMPOSEDID_ID));
detachedCriteria.setProjection(proj);
criteria = session.createCriteria(entityClazz)
.add( Subqueries.propertiesIn(new String[] { COMPOSEDID_VERSION_ID, COMPOSEDID_ID }, detachedCriteria));
此查询在Postgre Db中运行良好。但当我切换到MSSQL时,我得到以下错误:
Caused by: java.sql.SQLException: An expression of non-boolean type specified in a context where a condition is expected, near ','.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:671)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:505)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:1029)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)[201:org.hibernate.core:5.0.0.Final]
有人能帮我吗?为了实现针对每个Id获取maxVersion记录的目标,我应该在Criteria API中进行哪些更改???与其在分离的Criteria的投影条件中添加子查询,不如直接在Criteria中添加投影,如下所示:
DetachedCriteria detachedCriteria=DetachedCriteria.forClass(entityClazz);
ProjectionList proj = Projections.projectionList();
proj.add(Projections.max(COMPOSEDID_VERSION_ID));
proj.add(Projections.groupProperty(COMPOSEDID_ID));
criteria = session.createCriteria(entityClazz)
.setProjection( proj );
您是否打开show_查询来查看它试图做什么?这可能会有帮助。请看,这个方法对于简单的直接查询很好,我试图为MSSQL不支持的多个列的WHERE-IN子句实现它。