尽管执行SQL返回值

尽管执行SQL返回值

本文介绍了尽管执行SQL返回值,但Hibernate返回空值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将hibernate用作ORMapper。我想执行一个非常简单的hql查询:

I'm using hibernate as an ORMapper. I want to execute an actually rather simple hql query:

SELECT a
FROM Foo a
WHERE a.status = :A0status
ORDER BY a.bookingTypeCode ASC,
         a.priority ASC

然后,这个hql查询被转换成一个如下所示的sql查询:

This hql query is then converted into a sql query which looks something like this:

select a.*
from Foo a
where a.status='A'
order by a.bookingtypecode ASC,
         a.priority ASC

当我使用Oracle SQL Developer在oracle数据库上执行sql时,我返回了17行。但是,当我执行hql查询时(使用 Query 的列表方法),我得到一个包含所有 null 。尽管元素的数量是正确的,但没有一个元素实际加载。

When I execute the sql on the oracle database using the Oracle SQL Developer I get 17 rows returned. However, when I execute the hql query (using the list method of a Query I get a list of 17 elements that are all null. Although the number of elements is correct, not a single one of the elements is actually loaded.

这是我创建和执行查询的方式:

This is the way I create and execute my query:

// the hql query is stored in the hqlQuery variable;
// the parameter are stored in a Map<String, Object> called params
Query hQuery = hibSession.createQuery(hqlQuery);
for (Entry<String, Object> param : params.entrySet()) {
    String key = param.getKey();
    Object value = param.getValue();
    hQuery.setParameter(key, value);
}

List<?> result = hQuery.list();

有人知道这里可能存在什么问题吗?

Does anyone know what might be the problem here?

U更新1

我最近从hibernate 3.2升级到4.3.5。升级之前一切正常。升级后,我得到这个错误。

I've recently upgrade from hibernate 3.2 to 4.3.5. Before the upgrade everything worked fine. After the upgrade I get this error.

推荐答案

我将hibernate的日志级别设置为TRACE并发现问题。这实际上是一个映射/逻辑/数据库错误。主键由两列组成(根据实体类),其中一列可以为空。但是主键永远不能为空。因此,hibernate总是返回null。

I've set the Log level of hibernate to TRACE and found the problem. It was actually a mapping/logic/database error. The primary key consisted of two columns (according to the entity class) and one of these columns was nullable. However a primary key can never be nullable. Therefore hibernate always returned null.

这篇关于尽管执行SQL返回值,但Hibernate返回空值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:02