Just another blog about the Java programming language
Hibernate returning NULL entities?
Ever got NULL entity entries from Hibernate? I did.
Having a table V_MEAS_SITE with following attributes:
The V_ in the table name indicates that this is a Oracle view and not a table.
This name follows our naming conventions. As the name of the view indicates,
the view is holding all information about measurement sites (I actually work
for the national weather service). This table is mapped to the following Java class:
Let's write an EJB session bean reading some data:
That's easy. OK, let's test this mapping with a JUnit test class:
However, this test fails. There are indeed some NULL entity entries in the list.
Hibernate must be broken! Of course, Hibernate is not broken. Do you see what went wrong?
I did after some time of debugging. The view catches content of different tables and is
therefore de-normalized. The primary key of the V_MEAS_SITE view is not MEAS_SITE_ID
but INSTALLATION_ID!
As a view has no explicit contraints there is no primary key constraint neither and therefore
you have no explicit indications about the "logic" in the data. In this case it means that
there are more than one entry for a given measurement site ID. And that's why Hibernate
returned the NULL values.
Of course it would be nice, if Hibernate had thrown some Exceptions to help
a careless Java developer...
By the way, Oracle DB allows to query the DLL statement: