Java – operator of JPA like integer
Can someone tell me why this doesn't work:
criteria.add(cb.like((myentity.<Integer>get("integerid")).as(String.class),"2%"))
I received the following error:
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]
The only solution is to change the myinteger attribute to a string in the model so that I can use the like operator?
BR
Solution
JPA does not support like() with integers, only strings Some databases support integers, while others do not
Eclipse link should allow use of like () in integer (assuming your database supports it) What version are you using? You may need to use > = 2.1 If it fails on the latest version, log an error
You can also use the "char", "to_char" or "convert" functions to convert integers to strings, depending on your database The conditional API supports the function () API to call native functions
Note that the as () API is not used to convert from integer to string, but to subclass entities