I'm stumped, I think I may be missing something very obvious here..
anyways, query2 works just fine and works as I was expecting it to work, while query does not work, stacktrace below
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
StudyGuide studyGuide = new StudyGuide();
studyGuide.setId(1);
String startsWith = "a";
String query2 = "from GlossaryTerm as gt where gt.glossaryGroup.studyGuide.id = " + studyGuide.getId() +
" and upper(gt.name) like '" + startsWith + "%'";
session.find(query2);
String query = "from GlossaryTerm as gt where gt.glossaryGroup.studyGuide.id = ? and upper(gt.name) like '?%'";
session.find(query,
new Object[]{new Long(studyGuide.getId()), startsWith},
new Type[]{Hibernate.LONG, Hibernate.STRING});
Hibernate: select glossaryte0_.id as id, glossaryte0_.name as name, glossaryte0_.definition as definition, glossaryte0_.glossaryGroupId as glossary4_ from glossaryterm glossaryte0_, glossarygroup glossarygr1_ where (glossarygr1_.studyGuideId=1 and glossaryte0_.glossaryGroupId=glossarygr1_.id)and(upper(glossaryte0_.name)like 'a%' )
Hibernate: select glossarygr0_.id as id1_, glossarygr0_.name as name1_, glossarygr0_.studyGuideId as studyGui3_1_, studyguide1_.id as id0_, studyguide1_.name as name0_, studyguide1_.state as state0_ from glossarygroup glossarygr0_ left outer join studyguide studyguide1_ on glossarygr0_.studyGuideId=studyguide1_.id where glossarygr0_.id=?
Hibernate: select glossaryte0_.id as id, glossaryte0_.name as name, glossaryte0_.definition as definition, glossaryte0_.glossaryGroupId as glossary4_ from glossaryterm glossaryte0_, glossarygroup glossarygr1_ where (glossarygr1_.studyGuideId=? and glossaryte0_.glossaryGroupId=glossarygr1_.id)and(upper(glossaryte0_.name)like '?%' )
[WARN] JDBCExceptionReporter - SQL Error: 0, SQLState: S1009
[ERROR] JDBCExceptionReporter - Parameter index out of range (2 > 1).
[WARN] JDBCExceptionReporter - SQL Error: 0, SQLState: S1009
[ERROR] JDBCExceptionReporter - Parameter index out of range (2 > 1).
[ERROR] JDBCExceptionReporter - Could not execute query
java.sql.SQLException: Parameter index out of range (2 > 1).
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:1940)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:1075)
at com.mchange.v2.c3p0.impl.C3P0PreparedStatement.setString(C3P0PreparedStatement.java:83)
at net.sf.hibernate.type.StringType.set(StringType.java:26)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:48)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:35)
at net.sf.hibernate.loader.Loader.bindPositionalParameters(Loader.java:674)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:713)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:185)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:950)
at net.sf.hibernate.loader.Loader.list(Loader.java:941)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:834)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1512)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1491)
at els.test.TestHibernate.main(TestHibernate.java:78)
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 com.intellij.rt.execution.application.AppMain.main(Unknown Source)
net.sf.hibernate.JDBCException: Could not execute query
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1515)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1491)
at els.test.TestHibernate.main(TestHibernate.java:78)
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 com.intellij.rt.execution.application.AppMain.main(Unknown Source)
Caused by: java.sql.SQLException: Parameter index out of range (2 > 1).
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:1940)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:1075)
at com.mchange.v2.c3p0.impl.C3P0PreparedStatement.setString(C3P0PreparedStatement.java:83)
at net.sf.hibernate.type.StringType.set(StringType.java:26)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:48)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:35)
at net.sf.hibernate.loader.Loader.bindPositionalParameters(Loader.java:674)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:713)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:185)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:950)
at net.sf.hibernate.loader.Loader.list(Loader.java:941)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:834)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1512)
... 7 more
|