Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case-insensitive equals using Hibernate Criteria

Tags:

java

hibernate

I've seen Restrictions.ilike('property', '%value%'), but would like to generate SQL like: lower(property) = 'value'. Any ideas?

I used:

Restrictions.eq("email", email).ignoreCase() 

since Expression is deprecated. The SimpleExpression will call toLowerCase() on the value, so it is not necessary to do it beforehand.

See: SimpleExpression source

like image 789
jon077 Avatar asked Mar 09 '09 00:03

jon077


People also ask

Is hibernate case sensitive?

Case Sensitivity. Queries are case-insensitive, except for names of Java classes and properties. So SeLeCT is the same as sELEct is the same as SELECT but org. hibernate.

Can you explain criteria in hibernate?

Hibernate provides alternate ways of manipulating objects and in turn data available in RDBMS tables. One of the methods is Criteria API, which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical conditions.

Why do we use criteria in hibernate?

In Hibernate, the Criteria API helps us build criteria query objects dynamically. Criteria is a another technique of data retrieval apart from HQL and native SQL queries. The primary advantage of the Criteria API is that it is intuitively designed to manipulate data without using any hard-coded SQL statements.

What is the difference between criteria and criterion in hibernate?

Criteria criteria= session.createCriteria(Order.class) It can be used as restrictions on the criteria query. In other words, Criterion is the object-oriented representation of the “where” clause of a SQL query. The conditions to be applied (also known as restrictions) can be provided by the Restrictions class.


2 Answers

Expression is now deprecated. Use Restrictions instead ...

crit(Restrictions.eq("firstName", firstName).ignoreCase()); 
like image 60
Andy A Avatar answered Oct 15 '22 19:10

Andy A


Be careful of using ilike because it would allow someone to enter things like "test%" and match. I use the following to do a case-insensitive equal in one app:

... Criteria crit=session.createCriteria(Event.class); crit.add(Expression.eq("rsvpCode","test1").ignoreCase()); ... 
like image 37
John Wagenleitner Avatar answered Oct 15 '22 17:10

John Wagenleitner