Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does HQL have an equivalent for Restrictions.ilike (for case-insensitive matching)?

I wrote a project for Hibernate+MySQL. Now I'm porting it to Derby (for a number of reasons).

Now I discovered that Derby is case sensitive when using LIKE in queries. This could be solved using Restrictions.ilike(...) in Criteria queries... but I've many complex HQL queries that use that. Is there a way to have a functionality similar to ilike in HQL?

like image 566
gotch4 Avatar asked Nov 02 '11 08:11

gotch4


People also ask

Is HQL case sensitive?

HQL queries are case insensitive; however, the names of Java classes and properties are case-sensitive HQL is used to execute queries against database.

Is hibernate query case sensitive?

14.1.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.

What is HQL what makes it different from SQL?

Differences between SQL and HQL: SQL is based on a relational database model whereas HQL is a combination of object-oriented programming with relational database concepts. SQL manipulates data stored in tables and modifies its rows and columns. HQL is concerned about objects and its properties.

Is HQL and JPQL same?

The Hibernate Query Language (HQL) and Java Persistence Query Language (JPQL) are both object model focused query languages similar in nature to SQL. JPQL is a heavily-inspired-by subset of HQL. A JPQL query is always a valid HQL query, the reverse is not true however.


1 Answers

There is no ilike equivalent functionality in HQL. As Konstantin has already pointed out in his suggestion, your best choice is to tune the database connection and set collation to TERRITORY_BASED:SECONDARY, as explained in this JIRA: DERBY-1748: Global case insensitive setting.

Take into account that all equalities (=) and likes will be case insensitive. This might go a bit too far, and be not suitable for your particular situation.

Another way of addressing this would be creating function-based indexes (if Derby supports them, of course) and tune your HQL to combine like and lower like this.

Query q = session.createQuery("... WHERE lower(entity.field) like ?)");
q.setString(0, '%' + variable.toLowerCase() + '%');

If Derby doesn't support FBI's (I think it doesn't), you could also create trigger-filled columns with the lower values and index them.

UPDATE It seems to be possible to define derived/autogenerated columns, as explained in this other JIRA: JIRA-481: implement SQL generated columns.

like image 142
Xavi López Avatar answered Sep 19 '22 11:09

Xavi López