Java Spring中@Query中使用JPQL LIKE 写法

两种方式

// 一
public List<TestEntity> searchByJpql(){
    String jpql = "select k from TestEntity k where k.id = ?1 and k.no = ?2 and k.name like '%'||?3||'%' order by k.id asc";
    Object[] params = new Object[3];
    params[0]=Long.valueOf(id);
    params[1]=3L;
    params[2]="jobo";
    List<TestEntity> testEntity=testEntityDao.findByJpql(TestEntity.class, jpql, params);
}
// 二
@Query("select k from TestEntity k where k.id = ?1 and k.no = ?2 and k.name like '%'||?3||'%'")
public TestEntity searchOne(Long id, Long no, String name);

常见异常如下:

1.Cannot mix JPA positional parameters and native Hibernate positional/ordinal parameters;

原因:?1,?2 ,?3有错误;

2.Parameter with that position [2] did not exist;

原因:params中的参数和?1,?2 ,?3 不对应;

3.Parameter value [3] did not match expected type [java.lang.Long (n/a)];

原因:params中的参数和TestEntity中定义的变量类型不配陪,[3] 是指数组中的value值,而不是Object[3];

posted @ 2017-03-10 11:04  太潮了就要晒  阅读(5279)  评论(0编辑  收藏  举报