-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: query help
PostPosted: Wed Apr 07, 2004 10:03 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 07, 2004 10:20 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 6:59 pm
Posts: 89
Location: Somewhere in the Ghetto
ok I see whats happening now.. but I dont know if this a good way of fixing it,


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});

note the "%" appended to startsWith, is there a better way to handle this, or is this as good as im gonna get?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.