使用JPA 2.0 Criteria API并强制生成JPQL在Hibernate中失败 [英] Using JPA 2.0 Criteria API and cast causes generated JPQL to fail in Hibernate

查看:124
本文介绍了使用JPA 2.0 Criteria API并强制生成JPQL在Hibernate中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新JPA 2.0 Criteria API的首次使用者,当我需要将数字字段转换为字符串以将其与String参数进行比较时,我遇到了问题。原因是我想搜索部分数字,所以我在CriteriaBuilder上使用'like'。这里有一个代码示例:

  CriteriaBuilder cb = getEntityManager()。getCriteriaBuilder(); 
CriteriaQuery< ParcelDO> cq = cb.createQuery(ParcelDO.class);
Root< ParcelDO> parcelDO = cq.from(ParcelDO.class);
cq.select(parcelDO);

String parcelNumberId = parcelSearchDetailDO.getParcelNumberId(); (parcelNumberId!= null&&!parcelNumberId.isEmpty()){
Predicate parcelNumberIdPredicate = cb.like(
parcelDO。< Long> get(parcelNumberId)。 as(String.class),
parcelNumberId +%);

if(cq.getRestriction()!= null){
cq.where(cq.getRestriction(),parcelNumberIdPredicate);
} else {
cq.where(parcelNumberIdPredicate);


$ / code $ / pre
$ b $ p

重要部分是

  Predicate parcelNumberIdPredicate = cb.like(
parcelDO。< Long> get(parcelNumberId)。as(String.class),
parcelNumberId +%);

其中我使用Criteria API将Path转换为CriteriaBuilder上类似方法所需的表达式。

现在,当我运行并执行此代码时,基础JPA 2.0实现Hibernate失败,并出现以下异常:

 原因:org.hibernate.hql.ast.QuerySyntaxException:
期望关闭,找到'('在第1行第117列
[select generatedAlias0 from domain.ParcelDO as generatedAlias0 where
cast(generatedAlias0.parcelNumberId as varchar2(255 char))like:param0]

在我看来,Hibernate正在生成一个不正确的JPQL。



我不知道什么是错,你能帮忙吗?



我使用最新的Hibernate版本(3.6.0.CR2)

谢谢 <由于axtavt在问题的注释中指出,它是Hibernate 3.6中的一个bug nsource.atlassian.com/projects/hibernate/browse/HHH-5755rel =nofollow> http://opensource.atlassian.com/projects/hibernate/browse/HHH-5755


I am a first time user of the new JPA 2.0 Criteria API and I 'm running into a problem when I need to cast a number field to String to compare it with a String parameter. Reason is that I want to search for partial numbers, so I use a 'like' on the CriteriaBuilder. Here's a code sample:

CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<ParcelDO> cq = cb.createQuery(ParcelDO.class);
        Root<ParcelDO> parcelDO = cq.from(ParcelDO.class);
        cq.select(parcelDO);

        String parcelNumberId = parcelSearchDetailDO.getParcelNumberId();
        if (parcelNumberId != null && !parcelNumberId.isEmpty()) {
            Predicate parcelNumberIdPredicate = cb.like(
                    parcelDO.<Long> get("parcelNumberId").as(String.class),
                    parcelNumberId + "%");

            if (cq.getRestriction() != null) {
                cq.where(cq.getRestriction(), parcelNumberIdPredicate);
            } else {
                cq.where(parcelNumberIdPredicate);
            }
        }

The important part is

Predicate parcelNumberIdPredicate = cb.like(
                    parcelDO.<Long> get("parcelNumberId").as(String.class),
                    parcelNumberId + "%");

where I use the Criteria API to convert the Path into a Expression needed for the like method on the CriteriaBuilder.

Now when I run and it executes this code, the underlying JPA 2.0 implementation Hibernate fails with the following exception:

Caused by: org.hibernate.hql.ast.QuerySyntaxException:
expecting CLOSE, found '(' near line 1, column 117 
[select generatedAlias0 from domain.ParcelDO as generatedAlias0 where
cast(generatedAlias0.parcelNumberId as varchar2(255 char)) like :param0]

Looks to me like Hibernate is generating a JPQL that is not correct.

I have no idea what is wrong, can you help?

I use the latest Hibernate version (3.6.0.CR2)

Thank you

解决方案

As axtavt stated in the comments in the question, it is a bug in Hibernate 3.6 http://opensource.atlassian.com/projects/hibernate/browse/HHH-5755

这篇关于使用JPA 2.0 Criteria API并强制生成JPQL在Hibernate中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆