Hibernate is returning proxy objects even if no matching row is found on the database
-------------------------------------------------------------------------------------
Key: HHH-5302
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-5302
Project: Hibernate Core
Issue Type: Bug
Affects Versions: 3.5.1, 3.3.2
Reporter: Ovidiu Guse
Hi guys,
I have an issue with Hibernate and I don't know for sure if it is me doing something
wrong or this is a bug / "feature". Maybe someone has stepped into the same
problem and there is a way out.
Supposing I have a table 'Persons' with the normal structure: ID, first_name and
last_name, and the following records:
ID FIRST_NAME LAST_NAME
1 Jon Doe
2 Jane Doe
On my persistence service implementation (Spring based), I have a method like:
public Person findById(final Serializable id) {
return getHibernateTemplate().load(Person.class, id);
}
Now, everything is working fine if I'm passing to this method one of the valid IDs 1
or 2.
The problem appears when I'm passing for example -1 or 3, which do not exist at the
time of the query in the table. The result I'm getting is a proxy object with all
fields set to null, when in fact I was expecting the null value as result. And of course,
my code implements specific behavior depending on that operation result, for example if
the result is null, will throw an exception triggering some other execution paths.
First I guessed it was because of the javassist bytecode provider used by Hibernate, but
it turns out the same thing is happening with the cglib provider. This issue has appeared
when using Hibernate 3.2 GA, but is still there in Hibernate 3.5.1 Final.
The only (odd) difference between the two Hibernate version is that in Hibernate 3.2 the
proxy object returns the invalid id value used for search (e.g. person.getId() returns -1
or 3), while Hibernate 3.5.1 will throw an exception about "No row with given
identifier was found ..." when trying to read the object's ID value.
I have searched the Hibernate forums and came across these two links which are not
pointing to a valid solution:
http://opensource.atlassian.com/projects/hibernate/browse/EJB-167
http://opensource.atlassian.com/projects/hibernate/browse/HHH-1714
For now the ugly solution was to implement the toString() method on every domain object
model and after each search like this to log the result object. This will generate an
exception if the proxy result is not mapping a valid database row.
Is there a better way to handle this issue, or has somebody a solution in disabling this
"beautiful" Hibernate behavior?
Thanks,
Ovi
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira