-->
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.  [ 5 posts ] 
Author Message
 Post subject: Help with org.hibernate.Query
PostPosted: Thu Jun 29, 2006 9:33 am 
Newbie

Joined: Thu Jun 29, 2006 9:18 am
Posts: 4
I have a bit of code that I cant get to work when I use parameters but when I hard code the value the query works correctly. Below is my code. As written the result is an empty list. If I replace the '?' with a hardcoded '1' it works correctly. I verified that the dynamic value passed in is also a '1' it just doesnt seem to get set on the query. Any help?

******* CODE *******
StringBuffer select = new StringBuffer(
"SELECT person.events as event FROM PersonImpl as person");
StringBuffer where = new StringBuffer(" WHERE ");
int initLength = where.length();
int paramIndex = -1;
String joiner = " AND ";
if (person != null) {
where.append("person.id = ?" + joiner);
paramIndex++;
}
if (description != null) {
where.append("event.description = ?" + joiner);
paramIndex++;
}
int currLength = where.length();
if (currLength > initLength) {
select.append(where.toString().substring(0, currLength - joiner.length()));
}
Query query = sm.getSession().createQuery(select.toString());
if (person != null) {
// Long id = (Long)getPerson().getId();
// query.setParameter(0, id);
query.setLong(0, (Long)getPerson().getId());
}
if (description != null) {
query.setString(paramIndex, getDescription());
}
List results = query.list();
return results;
***** END CODE *****


ENVIRONMENT
Hibernate Version 3

Mysql 4.1

Drivers mysql-connector-java-3.1.8

Hibernate query log:
Hibernate: select eventimpl2_.id as id2_, eventimpl2_.firstOccurrence as firstOcc2_2_, eventimpl2_.lastOccurrence as lastOccu3_2_, eventimpl2_.reoccurrenceType as reoccurr4_2_, eventimpl2_.description as descript5_2_, eventimpl2_.person_fk as person6_2_ from person personimpl0_ inner join person_event events1_ on personimpl0_.id
=events1_.person_id inner join event eventimpl2_ on events1_.events_id=eventimpl2_.id where personimpl0_.id=?

Mysql query log:
060629 9:31:26 26 Prepare [3] select eventimpl2_.id as id2_, eventimpl2_.firstOccurrence as firstOcc2_2_, eventimpl2_.lastOccurrence as lastOccu3_2_, eventimpl2_.reoccurrenceType as reoccurr4_2_, eventimpl2_.description as descript5_2_, eventimpl2_.person_fk as person6_2_ from person personimpl0_ inner join person_event events1_ on personimpl0_.id=events1_.person_id inner join event eventimpl2_ on events1_.events_id=eventimpl2_.id where personimpl0_.id=?
26 Execute [3] select eventimpl2_.id as id2_, eventimpl2_.firstOccurrence as firstOcc2_2_, eventimpl2_.lastOccurrence as lastOccu3_2_, eventimpl2_.reoccurrenceType as reoccurr4_2_, eventimpl2_.description as descript5_2_, eventimpl2_.person_fk as person6_2_ from person personimpl0_ inner join person_event events1_ on personimpl0_.id=events1_.person_id inner join event eventimpl2_ on events1_.events_id=eventimpl2_.id where personimpl0_.id=?
26 Query commit



Thanks,
Kevin


Top
 Profile  
 
 Post subject: why not use named parameters?
PostPosted: Thu Jun 29, 2006 9:44 am 
Beginner
Beginner

Joined: Sat Jan 29, 2005 8:49 pm
Posts: 20
Code:
...
if (person != null) {
where.append("person.id = :personId" + joiner);
paramIndex++;
}
...
if (person != null) {
query.setLong("personId", (Long)getPerson().getId());
}
...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 10:04 am 
Newbie

Joined: Thu Jun 29, 2006 9:18 am
Posts: 4
That didnt solve it. I'm still getting the same behavior.

Though I thank you for your input thats defiantely a better way to write the query. I don't have to keep track of parameter indices that way.

K


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 10:06 am 
Newbie

Joined: Thu Jun 29, 2006 9:18 am
Posts: 4
New Code Same results:

******* CODE *******
StringBuffer select = new StringBuffer(
"SELECT person.events as event FROM PersonImpl as person");
StringBuffer where = new StringBuffer(" WHERE ");
int initLength = where.length();
String joiner = " AND ";
if (person != null) {
where.append("person.id = :personId" + joiner);
}
if (description != null) {
where.append("event.description = :description" + joiner);
}
int currLength = where.length();
if (currLength > initLength) {
select.append(where.toString().substring(0, currLength - joiner.length()));
}
Query query = sm.getSession().createQuery(select.toString());
if (person != null) {
query.setLong("personId", (Long)getPerson().getId());
}
if (description != null) {
query.setString("description", getDescription());
}
List results = query.list();
return results;

***** END CODE *****


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 10:34 am 
Newbie

Joined: Thu Jun 29, 2006 9:18 am
Posts: 4
Found the answer after some more searching on the Fourms:

http://forum.hibernate.org/viewtopic.ph ... uery+mysql

I dislike compatibility issues but its good that the forums can be helpful.

Kevin


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.