谁能告诉我,为什么这不起作用:
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]
是否是将 myinteger
属性更改为模型中的字符串的唯一解决方案,以便我可以使用 like
运算符?
BR
最佳答案
这里是它如何与 JPA 一起工作的示例,因此您可以在 JPQL 中将 LIKE 与整数一起使用。
public List<MyEntity> getMyEntities(String idStr) {
return entityManager.createQuery("SELECT m FROM MyEntity m WHERE CAST( m.id AS string ) LIKE :idStr")
.setParameter("idStr", "%" + idStr+ "%").getResultList();
}
Note: i tested it and worked fine with me, i am using JPA 2.1 with Hibernate provider.
关于java - 带有整数的 JPA 类运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5535084/