I think there is a bug in some Criteria-Criterions:
e.g. NotNullExpression: When I make an AliasProjection with the same name as the propertyName of the NotNullExpression, hibernate throws an exception. I think this exception occurs because referencing aliases in the WHERE-CLAUSE are not alowed in SQL (NotNullExpression.toSQLString() invokes CriteriaQuery.getColumnsUsingProjection() to use aliases for the where-clause).
Generated SQL: select distinct t2x1_.id as y0_ from test this_ inner join test t2x1_ on this_.id=t2x1_.test1_id where y0_ is not null
Exception: Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.loader.Loader.doList(Loader.java:2208) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2102) at org.hibernate.loader.Loader.list(Loader.java:2097) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:96) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1573) at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283) at org.hibernate.impl.CriteriaImpl$Subcriteria.list(CriteriaImpl.java:458) at test.TestApplication.main(TestApplication.java:52) Caused by: org.postgresql.util.PSQLException: ERROR: column "y0_" does not exist at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1592) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1327) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:192) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:451) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:350) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254) at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:184) at org.hibernate.loader.Loader.getResultSet(Loader.java:1785) at org.hibernate.loader.Loader.doQuery(Loader.java:674) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236) at org.hibernate.loader.Loader.doList(Loader.java:2205) ... 7 more
I think there is a bug in some Criteria-Criterions:
e.g. NotNullExpression:
When I make an AliasProjection with the same name as the propertyName of the NotNullExpression, hibernate throws an exception.
I think this exception occurs because referencing aliases in the WHERE-CLAUSE are not alowed in SQL (NotNullExpression.toSQLString() invokes CriteriaQuery.getColumnsUsingProjection() to use aliases for the where-clause).
Code:
List<?> result = s.createCriteria(Test.class, "t1")
.createCriteria("test1", "t2")
.add(Restrictions.isNotNull("id"))
.setProjection(Projections.distinct(Projections.property("t2.id").as("id")))
.list();
Generated SQL:
select distinct t2x1_.id as y0_ from test this_ inner join test t2x1_ on this_.id=t2x1_.test1_id where y0_ is not null
Exception:
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2208)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2102)
at org.hibernate.loader.Loader.list(Loader.java:2097)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:96)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1573)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at org.hibernate.impl.CriteriaImpl$Subcriteria.list(CriteriaImpl.java:458)
at test.TestApplication.main(TestApplication.java:52)
Caused by: org.postgresql.util.PSQLException: ERROR: column "y0_" does not exist
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1592)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1327)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:192)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:451)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:350)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:184)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1785)
at org.hibernate.loader.Loader.doQuery(Loader.java:674)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2205)
... 7 more