Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Hibernate] Select items starting with an specific letter

 
Miguel Flecha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. I want to do a select sentence that returns me all rows whose "APELLIDO" field starts with a specific letter that I have as parameter
How can I do that?
Regards
Miguel
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write "like" queries directly in HQL - "%" is your wildcard character.

Alternatively, you can create a Criteria with the like expression (check the JavaDocs for Expression and Criteria).
 
Miguel Flecha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
session.createQuery("from IbtUsuarios as u where u.login%'"+inicial+"*'")

this way?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. "%" is a wild character. "LIKE" is the comparator. Its just like you would do it in SQL.
 
Miguel Flecha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But do I have to search for 'a*', for example??
or is it another way

I used this and didn't work

session.createQuery("from IbtUsuarios as u where u.login LIKE '"+inicial+"*'").list()
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Close. '*' would be a wildcard if you are writing "SQL" for SQLServer. However in HQL (and ANSI-compliant SQL), its '%' (or '-' if you only wan't to match one character).
 
Miguel Flecha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah... ok ok, I didn't call that 'wild card'
I'll have a try
 
Miguel Flecha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works perfect. Thank you.
Can I make you another question about DB?
I am using MySQL and I want to store a password encrypted with Java Crypto API. I have used Varchar and it doesn't store it correctly, what type do I have to do?
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic