-->
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.  [ 3 posts ] 
Author Message
 Post subject: like with int columns
PostPosted: Tue Aug 21, 2007 9:37 am 
Newbie

Joined: Thu Jan 18, 2007 4:45 pm
Posts: 10
How can I make a query with like in hibernate for a integer column in the database. I tried but it gives me a class cast exception (java.lang.String cannot be cast to java.lang.Integer)

the objective is: when I search for the number 45 the result is:
45
450
1245
124598
How can I do it?
can I do it with the criteria API or I have to use HQL????

_________________
Dan Mopont


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 12:45 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Its pretty easy with HQL:
Code:
session.createQuery("from Entity where str(intPropertyName) like :val")
.setParameter("val", "%45%")
.list();


Can't see a way to apply an SQL function with the criteria API directly. Looks like you have to write your own custom Criterion:
Code:
session.createCriteria(Entity.class)
.add(new IntegerLikeExpression("intPropertyName", 45))
.list();


Here's a specific one covering your case:
Code:
public class IntegerLikeExpression implements Criterion {
   private String propertyName;
   private String likeValue;
   
   public IntegerLikeExpression(String propertyName, int likeValue) {
      this.propertyName = propertyName;
      this.likeValue = "%"+String.valueOf(likeValue)+"%";
   }
   
   public TypedValue[] getTypedValues(Criteria criteria,
         CriteriaQuery criteriaQuery) throws HibernateException {
      return new TypedValue[] { new TypedValue(Hibernate.STRING, likeValue, EntityMode.POJO) };
   }
   
   public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
         throws HibernateException {
      SQLFunction toStringFunc = criteriaQuery.getFactory().getSqlFunctionRegistry().findSQLFunction("str");
      List<String> funcArgs = new ArrayList<String>();
      funcArgs.add(propertyName);
      return toStringFunc.render(funcArgs, criteriaQuery.getFactory()) +" like ?";
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 1:22 pm 
Newbie

Joined: Thu Jan 18, 2007 4:45 pm
Posts: 10
Yes, with HQL it's easy, but i think that the Criteria form is better (even with more code). Thanks for the nice example... in my development I was using only the API functions... it's time to develop my own functions

thanks!

_________________
Dan Mopont


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