Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Can someone tell me, why this is not working:

criteria.add(cb.like((myentity.<Integer>get("integerid")).as(String.class), "2%"))

I get 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]

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
124 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here are example of how it work with JPA, so u can use LIKE with integer inside your JPQL.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...