Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate returns list of nulls although executed SQL returns values

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

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

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

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

Does anyone know what might be the problem here?

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

like image 662
mvieghofer Avatar asked Jul 09 '14 15:07

mvieghofer


People also ask

Does Hibernate Query List return null?

When there are no rows, both query. list() and criteria. list() are returning empty list instead of a null value.

How hibernate handle null values?

The best way to avoid Hibernate's attempts at setting null values to primitives is to use Wrapper classes (Integer, Long, Double...); and especially, if you need to tack on a column or 2 to an existing table. Auto-boxing is your friend.

What is query list Return hibernate?

list. Return the query results as a List. If the query contains multiple results pre row, the results are returned in an instance of Object[].


2 Answers

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.

like image 122
mvieghofer Avatar answered Sep 22 '22 10:09

mvieghofer


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?

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.

like image 21
Emmanuel Bernard Avatar answered Sep 22 '22 10:09

Emmanuel Bernard