jQuery in Action, 3rd edition
The moose likes Object Relational Mapping and the fly likes hibernate string search question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate string search question" Watch "hibernate string search question" New topic

hibernate string search question

Adrian Pang
Ranch Hand

Joined: Feb 20, 2004
Posts: 40

I'm a Hibernate newbie and I've a simple dumb question: I'm trying to construct a HQL statement for searching a database string field. What I want is kind of like:

select n.id from ContactName as n where n.name like attern%

so if the pattern is 'a', I get all the contacts whose name start with an A, for example. However, I can't seem to get this to work -- what's the correct syntax for this? I also tried to add % in the pattern string directly, but that doesn't work neither.

Thanks a lot!!!

SCJP 1.4, SCWCD 1.4, SCBCD 1.3
David Madouros

Joined: Mar 08, 2005
Posts: 18
If you want the actual ContactName objects back, try using any of the follwing:

Personally, I prefer the first example.

Also, using 'select n.id' will return a List of Long/Integer (depending on how you defined id) if that's what you were trying to do.
[ March 08, 2007: Message edited by: David Madouros ]

David Madouros<br />SCJP 1.4
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

I agree with David. Go with the first example.

Do not ever go with the third example, because it leaves you open for SQL based hacker attacks.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Adrian Pang
Ranch Hand

Joined: Feb 20, 2004
Posts: 40
Thank you!
I agree. Here's the link: http://aspose.com/file-tools
subject: hibernate string search question
It's not a secret anymore!