Java 尽管执行的 SQL 返回值,但 Hibernate 返回空值列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24657919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:52:20  来源:igfitidea点击:

Hibernate returns list of nulls although executed SQL returns values

javasqloraclehibernate

提问by mvieghofer

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

我使用 hibernate 作为 ORMapper。我想执行一个实际上相当简单的 hql 查询:

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

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

这个 hql 查询然后被转换成一个 sql 查询,它看起来像这样:

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

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 QueryI 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.

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

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();

The result I'm getting after executing query.list();

执行 query.list(); 后得到的结果;

Does anyone know what might be the problem here?

有谁知道这里可能有什么问题?

Update 1

更新 1

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.

我最近从休眠 3.2 升级到 4.3.5。升级前一切正常。升级后我收到此错误。

采纳答案by mvieghofer

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.

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

回答by Emmanuel Bernard

If you have not set a custom (and buggy) ResultTransformer, my second best guess is that your debugger is lying to you. Does you code actually receives a list of null?

如果您还没有设置自定义(和错误的)ResultTransformer,我的第二个最佳猜测是您的调试器在骗您。你的代码实际上收到一个空列表吗?

Also make sure to test with the code you are showing is. Too many times, people simplify things and the devil is in the details.

还要确保使用您显示的代码进行测试。太多时候,人们把事情简单化,而魔鬼在细节中。

回答by Eduardo Cordeiro

This error is happening to me. MySQL query browser works, but in hibernate of 7 columns and only one column always came with all null fields. I checked all the ids and they were not null. The error was in the construction of SQL Native. I had to change the way of writing it. Ai worked.

这个错误发生在我身上。MySQL 查询浏览器可以工作,但在 7 列的休眠状态下,只有一列总是带有所有空字段。我检查了所有的 ID,它们都不为空。错误出在 SQL Native 的构建中。我不得不改变写作的方式。艾工作了。

SELECT c.idContratoEmprestimo as idContratoEmprestimo,
c.dtOperacao as dataOperacao,
p.cpf as cpf,
p.nome as nome,
(Select count(p2.idParcelaEmprestimo) from EMP_PARCELA p2 where p2.valorPago > 0 and p2.dtPagamento is not null
and p2.idContratoEmprestimo = c.idContratoEmprestimo and p2.mesCompetencia <= '2014-08-01') as parcelasPagas, c.numeroParcelas as numeroParcelas,
pe.valorPago as valorParcela
FROM EMP_CONTRATO c inner join TB_PARTICIPANTE_DADOS_PLANO AS pp on pp.idParticipantePlano = c.idParticipantePlano
inner join TB_PARTICIPANTE as p on p.id = pp.idParticipante
inner join TB_PARTICIPANTE_INSTITUIDOR as pi on pi.PARTICIPANTE_ID = p.id
inner join EMP_PARCELA as pe on pe.idContratoEmprestimo = c.idContratoEmprestimo
where c.dtInicioContrato <= '2014-08-01' and pi.INSTITUIDOR_ID = 1
and c.avaliado is true
and pe.mesCompetencia = '2014-08-01'
and c.deferido is true
and c.dtQuitacao is null
and c.dtExclusao is null
and pe.valorPago is not null
group by c.idContratoEmprestimo
order by p.nome