JPA CriteriaBuilder like on double

℡╲_俬逩灬. 提交于 2020-08-08 18:51:26

问题


I'm trying to retrieve data out of a legacy database.

The column in the table is defined as a DECIMAL(13,0) containing account numbers.

The column data type cannot be changed as it will have a major impact on the legacy system. Essentially all programs using the table need to be changed and then recompiled which is not an option.

We have a requirement to find all records where the account number contains a value, for example the user could search for 12345 and all accounts with an account number containing 12345 should be returned.

If this was a CHAR/VARCHAR, I would use:

criteriaBuilder.like(root.<String>get(Record_.accountNumber), searchTerm)

As a result of the column defined as DECIMAL(13,0), the accountNumber property is a double.

Is there a way to perform a like on a DECIMAL/double field?

The SQL would be

SELECT * FROM ACCOUNTS WHERE accountNumber LIKE '%12345%'

回答1:


I have not actually tried this, but I believe it should work

criteriaBuilder.like(
    root.get(Record_.accountNumber).as(String.class),
    searchTerm)

This should generate a query kind of like this:

SELECT * FROM ACCOUNTS WHERE CAST(accountNumber AS text) LIKE '%12345%'


来源:https://stackoverflow.com/questions/43603951/jpa-criteriabuilder-like-on-double

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!