Reputation: 2342
I'm trying to get the result by sending the url query param in Japanese like below. Sample url is https://xyz/accounts?name=お金
Is it a followed practice that sending query params in Japanese or other languages. If it is what changes should be made in the following code? The below code has already been tested and it works for query params values in English like name=data.
Also, If i do a query to a database it works perfectly fine and give the result back.
select a.id, a.plan, p.name from account a inner join plan p on a.plan=p.id where (a.id in (1, 2 , 3)) and p.name='お金' order by p.name asc;
But when passing this query param value using criteria query it just gives an empty array.
public List<AccountEntity> createCriteriaQuery(final List<String> accountIdList,
final MultiValueMap<String, String> allRequestParams) {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<AccountEntity> cq = cb.createQuery(AccountEntity.class);
final Root<AccountEntity> accountEntity = cq.from(AccountEntity.class);
final Join<AccountEntity, PlanEntity> account = accountEntity.join(AccountEntity_.planEntity);
final List<Predicate> predicates = new ArrayList<Predicate>();
// Default Predicates for accounts
predicates.add(accountEntity.get(AccountEntity_.id).in(accountIdList));
if (!allRequestParams.isEmpty()) {
if (allRequestParams.containsKey(ApplicationConstants.QUERYPARAM_PLANNAME)) {
final String planName = allRequestParams.get(ApplicationConstants.QUERYPARAM_PLANNAME).get(0);
// For planName we check for "contains" which is sql like query
predicates.add(cb.like(account.get(PlanEntity_.name), "%" + planName + "%"));
}
}
It gets added to the predicates, when I debug the app I can see that its been added exactly the same as whats been passed in the url, but I guess it fails to create the query for some reason in the below steps. Also when I print planName i'm getting ??
cq.select(accountEntity).where(predicates.toArray(new Predicate[] {}));
cq.orderBy(cb.asc(account.get(PlanEntity_.name)));
final TypedQuery<AccountEntity> qry = entityManager.createQuery(cq);
final List<AccountEntity> accountEntityList = qry.getResultList();
//So I get accountEntityList as []
entityManager.close();
return accountEntityList;
}
Let me know if I need to provide any other details.
Upvotes: 1
Views: 430
Reputation: 2342
This solved the issue. We are using Spring boot, I added this(?useUnicode=yes&characterEncoding=UTF-8) in my application.yml file and it worked.
spring:
datasource:
url:jdbc:mysql://localhost:3306/mydb?useUnicode=yes&characterEncoding=UTF-8
If you have application.properties then,
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=yes&characterEncoding=UTF-8
Upvotes: 1