JPA Like Integer运算符 [英] JPA Like Operator with Integer
问题描述
有人可以告诉我,为什么这不起作用:
Can someone tell me, why this is not working:
criteria.add(cb.like((myentity.<Integer>get("integerid")).as(String.class), "2%"))
我收到以下错误:
The object [2%], of class [class java.lang.String], from mapping [org.eclipse.persistence.mappings.DirectToFieldMapping[integerid-->MYENTITY.INTEGERID]] with descriptor [RelationalDescriptor(org.example.model.MyEntity --> [DatabaseTable(MYENTITY)])], could not be converted to [class java.lang.Integer]
是唯一可以改变<$的解决方案c $ c> myinteger 属性为模型中的字符串,以便我可以使用之类的
运算符?
Is the only solution to change the myinteger
property to a string in the model, in order that I can use the like
operator?
BR
推荐答案
JPA不支持带有整数的like(),只支持字符串。有些数据库确实支持整数,有些数据库不支持。
JPA does not support like() with Integers, only Strings. Some databases do support like with integer and others do not.
EclipseLink应该允许在Integer中使用like()(假设你的数据库支持它)。你用的是什么版本?可能需要使用> = 2.1。如果它在最新版本上失败,那么请记录一个错误。
EclipseLink should allow usage of like() with Integer (provided your database supports it). What version are you using? May need to use >= 2.1. If it fails on the latest version, then please log a bug.
您还可以使用CHAR,TO_CHAR或CONVERT将整数转换为字符串函数取决于您的数据库。条件API支持函数()API来调用本机函数。
You can also convert the integer to a string using a "CHAR, "TO_CHAR" or "CONVERT" function depending on your database. The criteria API supports a function() API to call a native function.
请注意,as()API不适用于从Integer转换to String,用于转换为子类实体。
Note that the as() API is not intended for converting from Integer to String, it is for casting to a subclass entity.
这篇关于JPA Like Integer运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!