Hibernate returns a list of zeros, although running SQL returns values

I am using hibernate as an ORMapper. I want to execute an actually pretty simple hql query:

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

This hql query is then converted to a sql query that looks something like this:

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

When I execute sql in the oracle database using Oracle SQL Developer, I get 17 rows. However, when I execute the hql query (using the list of method Query, I get a list of 17 elements, which are all null. Although the number of elements is correct, none of the elements are loaded.

So I create and execute my request:

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

Does anyone know what could be the problem here?

Update 1

I recently upgraded from hibernate 3.2 to 4.3.5. Before the update, everything worked fine. After updating, I get this error.

+4
4

TRACE . // . ( ), . . hibernate null.

+7

( ) ResultTransformer, , . ?

, , . , .

+1

This mistake is with me. MySQL query browser works, but in sleep mode of 7 columns and only one column is always with all zero fields. I checked all the identifiers and they were not empty. The error was in building SQL Native. I had to change the way I write. Ai worked.

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
0
source

Thanks a lot, I have the same problem. I dug deep and found that when sending nulla, the query parameter is requesting the wrong result.

	
	@RequestMapping(method = RequestMethod.POST, value = "/getUserFlatTxnDetails")
	public List<UserFlatTxnDetailsDto> getUserFlatTxnDetails(@QueryParam("Integer") Integer smId){
		List<UserFlatTxnDetailsDto> userFlatTxnList = new ArrayList<>(); 
		userFlatTxnList = userService.getUserFlatTxn(smId); 
		return userFlatTxnList;
	}
	
  public List<UserFlatTxnDetailsDto> getUserFlatTxn(Integer smId) {
		Session session = sessionFactory.openSession();
//		LOGGER.info("getUserFlatTxn session" + session);
		Transaction transaction = session.beginTransaction();
		Query query = session.createSQLQuery(UserConst.GET_USERS_FLAT_TXN_QUERY);
		query.setParameter("smId", smId);
		List<UserFlatTxnDetailsDto> uftList = new ArrayList<>();
		List<Object[]> list = query.list();
		createUserFlatTxnDtoList(uftList, list);
		transaction.commit();
		session.close();
		return uftList;
	}
Run codeHide result
-1
source

Source: https://habr.com/ru/post/1547781/


All Articles