Hibernate returns list of nulls although executed SQL returns values
Solution 1
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.
Solution 2
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.
Related videos on Youtube
Comments
-
mvieghofer almost 2 years
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 allnull
. 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?
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.
-
mvieghofer almost 10 yearsHi, no I did not set acustom ResultTransformer and the list returned is actually a list of nulls (I'm getting a NullPointerException because of this). Also the SQL I've posted above is more or less exactly the SQL that is generated by hibernate (I just simplified it by selecting * and changing the alias).
-
Jeremy over 7 yearsWorked for me, I had switched the column by which I needed to annotate with
@Id
in my domain class that hibernate was mapping DB objects into. In my case my table did not have a primary key, but the annotation still caused the nulls because hibernate doesn't want a null ID even in the instances it creates from the DB query results regardless if its a primary key or not. -
banan3'14 about 2 yearsI had to set the log level to
FINEST
, it was a problem with the query in my case. For anyone wondering how to set the log level stackoverflow.com/a/847121/7769052