我想在JPQL中使用LIKE關鍵字過濾BigDecimal標識。我使用EclipseLink。將BigDecimal轉換爲字符串以便在JPQL查詢中使用LIKE過濾一些BigDecimal標識
假設我們有ID:197718,182123,182912,123456,並且我想從表中提取entity.id(如%18%)的所有行。如果操作成功,則結果將爲:182123,182912。
我的查詢會。
public List<MyEntity> getFilteredMyEntity(String idStr) {
return entityManager.createQuery("select m from MyEntity m where m.id like :idStr")
.query.setParameter("idStr", "%" + idStr+ "%")
.getResultList();
}
讓我們說,我調用這個方法這樣才能得到一個包含「18」的ID的所有實體:
List<MyEntity> entities = getFilteredMyEntity("18");
但我得到一個例外,因爲我不能比較的BigDecimal與一個字符串。所以,我必須將此BigDecimal轉換爲字符串
有沒有在jpql(非本機查詢)將BigDecimal轉換爲字符串的方法嗎?類似於to_char()。
'String#valueOf'和'BigDecimal#toString'似乎是很明顯的選擇。那些不適合你? –
[JPQL like表達式需要int值](http://stackoverflow.com/q/22558476/5221149)或[JPA Like Operator with Integer](http://stackoverflow.com/q/5535084/5221149) 。兩者都表示同樣的事情:在查詢文本中將'm.id'強制轉換爲字符串,所以'like'在兩側都有字符串。 – Andreas
請檢查我的答案,我使用JPQL測試它,它工作正常 –