-->
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.  [ 6 posts ] 
Author Message
 Post subject: Unable to A Simple Search
PostPosted: Wed Nov 29, 2006 2:48 pm 
Newbie

Joined: Wed Nov 29, 2006 2:28 pm
Posts: 12
Hibernate version:3.2

I have a table in which the ID is a string of length 3. When I attempt to read a record based on the ID the record is not found (null is returned).

I know the record is there because:
1.) When I use Hibernate to get a list of all the records in the table I see the one I am looking for
2.) When I cut and past the Hibernate generated SQL into SQL Plus the querry works!

Any ideas?

code:
Code:
   public static ReviewStatusType findById(Session hSession, String Id)
   throws HibernateException {
      ReviewStatusType reviewStatusType = null;
      try {
         Criteria crit = hSession.createCriteria(ReviewStatusType.class);
         crit.add(Expression.eq("reviewStatusTypeId", Id));
         reviewStatusType = (ReviewStatusType) crit.uniqueResult();
      } catch (HibernateException he) {
         throw (he);
      }
      return reviewStatusType;
   }   


Mapping documents:
<id name="reviewStatusTypeId" type="string">
<column name="REVIEW_STATUS_TYPE_ID" length="3" />
<generator class="assigned" />
</id>

Name and version of the database you are using:Oracle 10g

The generated SQL (show_sql=true):
select this_.REVIEW_STATUS_TYPE_ID as REVIEW1_85_0_, this_.NAME as NAME85_0_, this_.LAST_UPDATE_USERID as LAST3_85_0_, this_.LAST_UPDATE_TS as LAST4_85_0_, this_.SORT_SEQUENCE as SORT5_85_0_
from CWNS2008.REVIEW_STATUS_TYPE this_
where this_.REVIEW_STATUS_TYPE_ID=?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 6:22 am 
Beginner
Beginner

Joined: Tue Nov 21, 2006 5:18 am
Posts: 31
Location: Bangalore, India
Hi,

Check your Id value is having any space in the database.

_________________
persist_coder
--credit please if it helps you


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 8:11 am 
Newbie

Joined: Wed Nov 29, 2006 2:28 pm
Posts: 12
Yes, that was it.

In the DB the column was defined as char (3 Bytes). The record I was looking for had a key value of "SH ". When Hibernate executed the query it failed because I was searching for "SH" (no trailing blank) but when I cut and pasted the Hibernate generated query into SQL Plus it worked.

I believe the reason it did not work in Hibernate but did work when I cut and paste the query is that Hibernate uses paramerized queries and they must behave differently.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 8:12 am 
Newbie

Joined: Wed Nov 29, 2006 2:28 pm
Posts: 12
Yes, that was it.

In the DB the column was defined as char (3 Bytes). The record I was looking for had a key value of "SH ". When Hibernate executed the query it failed because I was searching for "SH" (no trailing blank) but when I cut and pasted the Hibernate generated query into SQL Plus it worked.

I believe the reason it did not work in Hibernate but did work when I cut and paste the query is that Hibernate uses paramerized queries and they must behave differently.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 10:35 am 
Beginner
Beginner

Joined: Tue Nov 21, 2006 5:18 am
Posts: 31
Location: Bangalore, India
Yes, hibernate takes the which you give as whole value, like parameterized one.

_________________
persist_coder
--credit please if it helps you


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 11:30 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
I believe CHAR always right pads with whitespaces to the maximum length that its defined for, whereas VARCHAR only uses how much the actual size is. So try changing your DB to use VARCHAR and your original SQL should work.


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