Monday, December 20, 2004

Hibernate Mapping issue !

I led a team of developers implementing Hibernate for a project. There was this developer who always managed to get stuck in something very silly.

So one day he comes to me declaring , "Hibernate does not work at all", i have doen everthing as per the spec and still it does notw work.

I saw his code and mapping and it seemed very fine! , i spent 4 hours debugging what he had done. In the process i opened up the hibernate source code (release 1.4) .

What i found was indeed silly.

This developer had done the following Hibernate mapping:-
.....
....

Looks innocent does it not ? Well look again, there is a space
column="CORPORATION_ID " , and this is what hibernate did not like and gave the error will get the error that "JDBC error , column name not found...".

Here is the code for the Integer value Type from hibernate.

public class IntegerType extends PrimitiveType implements DiscriminatorType, VersionType {

private static final Integer ZERO = new Integer(0);

public Object get(ResultSet rs, String name) throws SQLException {
return new Integer(rs.getInt(name));
}
....
The error would not exist if the method was returning like this:-

return new Integer(rs.getInt(name.trim()));

I did post this some time ago on the hibernate forum :-

http://forum.hibernate.org/viewtopic.php?t=926430

I got a resonse that trim is not needed. I sort of agree that a developer should not be stupid to put spaces in the hibernate mapping files, but also i think having a trim(), does no harm.
At least that way i could have saved some time debugging.

Lastly i love open source code, as that way i know exactly what the issue is, rather than having to imagine what the problem maybe, or waiting for technical support.

0 Comments:

Post a Comment

<< Home