Hi,
The problem below occurs with both Hibernate 3.0.5 and Hibernate 3.1 rc2.
I tested it with two different databases: Sybase and HSQL DB and the same problem occurs.
It seems that there is a problem when you use the Criteria API and when there is a filter enabled.
The problem is an invalid argument in JDBC call: parameter index out of range.
Here is the mapping file:
Code:
<hibernate-mapping package="test.hibernate.mapping">
<class name="TransactionImpl" table="test_transaction" mutable="false">
<id name="id" column="id" type="java.lang.Integer" >
<generator class="hilo"/>
</id>
<property name="client" column="client" type="java.lang.String"/>
<property name="account" column="account" type="java.lang.String"/>
<property name="otherData" column="other_data" type="java.lang.String"/>
<filter name="ClientFilter"
condition="client in (
select fd.entity_id from test_filterdata fd where
fd.username=':username' and fd.namespace='ClientNameSpace' and fd.entitygroup_id='Client')"/>
</class>
<class name="FilterDataImpl" table="test_filterdata" mutable="false">
<id name="id" column="id" type="java.lang.Long">
<generator class="hilo"/>
</id>
<property name="username" column="username" type="java.lang.String" not-null="true" />
<property name="namespace" column="namespace" type="java.lang.String" not-null="true" />
<property name="entityGroupId" column="entitygroup_id" type="java.lang.String" not-null="true" />
<property name="entityId" column="entity_id" type="java.lang.String" not-null="true" />
</class>
<filter-def name="ClientFilter">
<filter-param name="username" type="java.lang.String"/>
</filter-def>
</hibernate-mapping>
Here is a test program:
Code:
Session session = getSession();
// enable filter
session.enableFilter("ClientFilter").setParameter("username", "user");
// use Criteria API to do a search
Collection result = session.createCriteria(TransactionImpl.class).list();
System.out.println("result size: " + result.size());
And here is the exception stack trace with Sybase:
Code:
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2150)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026)
at org.hibernate.loader.Loader.list(Loader.java:2021)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1483)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
at test.hibernate.HibernateFilterTest.test1(HibernateFilterTest.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:436)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:311)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.sql.SQLException: JZ0SB: Parameter index out of range: 1.
at com.sybase.jdbc3.jdbc.ErrorMessage.raiseError(ErrorMessage.java:552)
at com.sybase.jdbc3.jdbc.ParamManager.checkIndex(ParamManager.java:324)
at com.sybase.jdbc3.jdbc.ParamManager.setParam(ParamManager.java:374)
at com.sybase.jdbc3.jdbc.SybPreparedStatement.setParam(SybPreparedStatement.java:1191)
at com.sybase.jdbc3.jdbc.SybPreparedStatement.setParam(SybPreparedStatement.java:1184)
at com.sybase.jdbc3.jdbc.SybPreparedStatement.setString(SybPreparedStatement.java:288)
at org.hibernate.type.StringType.set(StringType.java:26)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:85)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:63)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1514)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1576)
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
at org.hibernate.loader.Loader.doList(Loader.java:2147)
... 21 more
Code:
And same with HSQL in-memory database:
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2150)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026)
at org.hibernate.loader.Loader.list(Loader.java:2021)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1483)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
at test.hibernate.HibernateFilterTest.test1(HibernateFilterTest.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:436)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:311)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.sql.SQLException: Invalid argument in JDBC call: parameter index out of range: 1
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.checkSetParameterIndex(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.setParameter(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.setString(Unknown Source)
at org.hibernate.type.StringType.set(StringType.java:26)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:85)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:63)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1514)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1576)
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
at org.hibernate.loader.Loader.doList(Loader.java:2147)
... 21 more
Is it a bug ?
Any workaround ?
Thanks
Patrick