ripper234
ripper234

Reputation: 230038

How can I use percent in a LIKE clause in JPA/Play Framework?

I want to find all Users whose email contain the string middle somewhere inside.

The following code fails - it seems JPA doesn't recognize the ? as a parameter because its enclosed by quotation marks.

List<User> users = User.findAll("email like '%?%'", middle);

The following code works, but is open to SQL injections:

List<User> users = User.findAll("email like '%" + middle + "%'");

What would you suggest?

Upvotes: 2

Views: 857

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

Try moving your string manipulation to the Java side, and keep the SQL parameter:

List<User> users = User.findAll("email like ?", "%"+middle+"%");

Upvotes: 3

Related Questions