• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

prepared statements in hibernate

 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys

i need to do a search .The requirement for this arise to me when i need to do search operation using LIKE statement
in which i may pass string like for example 'test's' to LIKE statement of sql

select * from product where product_name LIKE'%searchString% '

how can i write prepared statement for this scenario using hibernate

regards
amir
[ February 27, 2008: Message edited by: Amirtharaj Chinnaraj ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HQL supports LIKE, with the same syntax as SQL.

Restrictions also support like methods, so you can do the same with Criteria based queries.
[ February 27, 2008: Message edited by: Paul Sturrock ]
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the Criteria API.

Here's a little something I'm working on. It might give you some ideas:


I�m always amazed when the database gurus I know get in front of the awful DB2 Control Console and start whipping out all sorts of crazy SQL statements that use weird escape characters to do nutty like and matching queries. Those skills are simply beyond my grasp. That�s why I was so excited to see that the Example class in the criteria API makes it incredibly easy to issue �like� or �fuzzy matching� queries by simply invoking a few methods of the API.

For example, say I wanted to find all of the users whose email address ended with .com? It�d be easy! All I�d have to do is create a criteria query that uses an example User instance, and have that instance�s email property initialized to .com. Of course, simply initializing the email property to .com won�t return any results, because there is no email address in my database that is exclusively .com. However, if I invoke the enableLike() method, and set the MatchMode to END, the criteria query will return all of the results where the end of the email address is .com Pretty neat, eh?


static MatchMode ANYWHERE
Match the pattern anywhere in the string
static MatchMode END
Match the end of the string to the pattern
static MatchMode EXACT
Match the entire string to the pattern
static MatchMode START
Match the start of the string to the pattern








-Cameron McKenzie
[ February 27, 2008: Message edited by: Cameron Wallace McKenzie ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic