关于spring data jpa布尔类型条件查询异常

xiatianlong
  • 2.2k

entity:

 /**
 * 是否删除
 */
private boolean delete;

 /**
 * 获取 是否删除
 */
@Column(name = "IS_DELETE", nullable = false)
public boolean isDelete() {
    return this.delete;
}

/**
 * 设置 是否删除
 */
public void setDelete(boolean delete) {
    this.delete = delete;
}

jpa查询:

// 创建时间倒序
Sort.Order order = new Sort.Order(Sort.Direction.DESC, "articleId");
Pageable pageable = new PageRequest(articlePageForm.getPageNo() - 1, articlePageForm.getPageSize(), new Sort(order));
Specification<ArticleEntity> specification = new Specification<ArticleEntity>() {
    @Override
    public Predicate toPredicate(Root<ArticleEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

        List<Predicate> predicateList = new ArrayList<>();

        predicateList.add(cb.equal(root.get("userId"), "100001"));
        predicateList.add(cb.equal(root.<Boolean>get("delete"), false));

        Predicate[] pre = new Predicate[predicateList.size()];
        return cb.and(predicateList.toArray(pre));
    }
};

关键在于这一行:

predicateList.add(cb.equal(root.<Boolean>get("delete"), false));

去掉就没问题,感觉是boolean值的问题,相关文档也没找到,请教这样的问题怎么解决?

异常信息:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: generatedAlias0 near line 1, column 136 [................]

难道delete属性的get方法应该是 getDelete() ???!!!

回复
阅读 5.6k
2 个回答

root.<Boolean>get("delete").as(Boolean.class)

Yujiaao
  • 12.6k

把关键字设成属性字段是不是该算是自寻烦恼,建议你把delete改成deleted, 生活会变得轻松很多。

@Column(name = "IS_DELETE" 《==》get("delete")
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏