本文介绍了EclipseLink无法获取标量布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下JPA条件查询在Hibernate上成功运行(最终版本为4.2.7).

The following JPA criteria query succeeds on Hibernate (4.2.7 final).

CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Boolean>criteriaQuery=criteriaBuilder.createQuery(Boolean.class);
Root<UserTable> root = criteriaQuery.from(entityManager.getMetamodel().entity(UserTable.class));
criteriaQuery.multiselect(root.get(UserTable_.enabled));

ParameterExpression<String>parameterExpression=criteriaBuilder.parameter(String.class);
criteriaQuery.where(criteriaBuilder.equal(criteriaBuilder.lower(criteriaBuilder.trim(root.get(UserTable_.emailId))), criteriaBuilder.lower(criteriaBuilder.trim(parameterExpression))));
List<Boolean> list = entityManager.createQuery(criteriaQuery).setParameter(parameterExpression, "admin").getResultList();

for(Boolean o:list)
{
    System.out.println("enabled : "+o);
}

这仅仅是为了从MySQL数据库返回标量布尔值.对应的列在MySQL中的类型为TINYINT(1).

It is simply meant to return a scalar Boolean value from MySQL database. The corresponding column is of type TINYINT(1) in MySQL.

它将生成以下SQL语句.

It generate the following SQL statement.

SELECT
    usertable0_.enabled as col_0_0_
FROM
    social_networking.user_table usertable0_
WHERE
    lower(trim(BOTH FROM usertable0_.email_id))=lower(trim(BOTH FROM ?))


同一查询在EclipseLink(2.5.1)上失败,在这种情况下,不返回任何内容(无错误,无异常).但是,它会生成如下正确的SQL语句.


The same query fails on EclipseLink (2.5.1) in which case it returns nothing (no error, no exception). It however, generates a correct SQL statement as follows.

SELECT enabled
FROM projectdb.user_table
WHERE (LOWER(TRIM(email_id)) = LOWER(TRIM(?)))

bind => [admin]

对应的JPQL就像这样

The corresponding JPQL like so,

SELECT u.enabled
FROM UserTable u
WHERE lower(trim(u.emailId))=lower(trim(:emailId))

也没有得到有问题的值.

also doesn't get the value in question.

但是,它可与其他列配合使用,如下所示.

It however, works with additional columns as shown below.

CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Object[]>criteriaQuery=criteriaBuilder.createQuery(Object[].class);
Root<UserTable> root = criteriaQuery.from(entityManager.getMetamodel().entity(UserTable.class));
criteriaQuery.multiselect(root.get(UserTable_.enabled), root.get(UserTable_.firstName));

ParameterExpression<String>parameterExpression=criteriaBuilder.parameter(String.class);
criteriaQuery.where(criteriaBuilder.equal(criteriaBuilder.lower(criteriaBuilder.trim(root.get(UserTable_.emailId))), criteriaBuilder.lower(criteriaBuilder.trim(parameterExpression))));
List<Object[]> list = entityManager.createQuery(criteriaQuery).setParameter(parameterExpression, userName).getResultList();

添加了另外一列root.get(UserTable_.firstName),并且CriteriaQuery的返回类型从CriteriaQuery<Boolean>更改为CriteriaQuery<Object[]>.

One extra column root.get(UserTable_.firstName) is added and the return type of CriteriaQuery is changed from CriteriaQuery<Boolean> to CriteriaQuery<Object[]>.

该列在相应的实体中定义如下.

The column is defined as follows in the corresponding entity.

@Basic(optional = false)
@NotNull
@Column(name = "enabled")
private Boolean enabled;  //Getter and setter.

为什么在EclipseLink中没有给出布尔值?

Why doesn't it given a Boolean value in EclipseLink?

推荐答案

这是由于EclipseLink错误 https://bugs.eclipse.org/bugs/show_bug.cgi?id=340089 ,这导致EclipseLink无法将SQL查询返回的值与其内部使用的构造区分开来没有结果.选择另一个值是唯一的解决方法,但是看起来很简单,几乎没有人点击它,或者只是在不评论或对该错误投票的情况下解决它.

This is due to EclipseLink bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=340089 which causes EclipseLink to be unable to distinguish the value returned from the SQL query from the construct it uses internally to indicate there were no results. Selecting another value is the only workaround, but it seems simple enough that not many people hit it or just workaround it without commenting or voting for the bug.

这篇关于EclipseLink无法获取标量布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 22:57