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.  [ 4 posts ] 
Author Message
 Post subject: Problem when using LikeExpression
PostPosted: Wed Jun 25, 2008 12:12 pm 
Newbie

Joined: Tue Jan 15, 2008 5:48 am
Posts: 4
Hi,

I'm using hibernate 3.2.5.ga on Oracle 10.

I've a problem when I try to use LikeExpression :

I try two solutions which give the same SQL but not the same result :

my first solution :
Criteria crit =
session.createCriteria(MyObject.class).add(Restrictions.like("name", data + "%"));

my second (is for escape specialy character):
LikeExpression likeExp = new LikeExpression("name", data + "%");

Criteria crit =
session.createCriteria(MyObject.class).add((Criterion) likeExp));


In my BDD I have two objects with name begin by 'toto'. The first solution returns my two objects but the second not.

Have you any Ideas to help me.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 3:20 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I always just like using the MatchMode:

Example example = Example.create(user);
example.enableLike(MatchMode.END);


http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=09howtousethecriteriaapi

Code:
public static void main(String[] args) {
  User user = new User();
  user.setEmailAddress(".com");
  Example example = Example.create(user);
  example.enableLike(MatchMode.END);
  Session session = HibernateUtil.beginTransaction();
  Criteria criteria = session.createCriteria(User.class);
  criteria.add(example);  List results = criteria.list();
  for (int i = 0; i<results.size(); i++) {
    System.out.println(results.get(i).toString());
  }
  HibernateUtil.commitTransaction();
}

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 4:53 am 
Newbie

Joined: Tue Jan 15, 2008 5:48 am
Posts: 4
Thank's a lot, it works


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 9:32 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Don't be afraid to rate the post. ;)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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