max wrote:
map it to java.lang.Integer (integer is the same as int, just takes longer to type ;)
Ahh, I see... I actually figured out why it wasn't picking the mapping up, and it was just because I didn't understand the SQL Types correctly and was trying to map NUMBER length="5" precision="0" rather than NUMBER precision="5" scale="0". For some reason I had misunderstood what "Hibernate type" was referring to and thought it was some type of mapping itself that would map to java.lang.Integer.
Once I realized my mistake with the mapping definition, I did try mapping to java.lang.Integer, however that was breaking everything that was already using those classes so I removed the mapping overrides and tried a different approach. Since I was already using a customized template for the DAO's, I changed the get() call in findById() to test the type of id and if it's a primitive to create the appropriate wrapper class for that type.
Code:
<#if c2j.isPrimitive(c2j.getJavaTypeName(clazz.identifierProperty, jdk5))>
<#if c2j.getJavaTypeName(clazz.identifierProperty, jdk5).equals("int") ||
c2j.getJavaTypeName(clazz.identifierProperty, jdk5).equals("integer")>
<#assign safeId = "new Integer(id)">
<#elseif ...>
It really seems like this is something that should be being done in the default template since if id is a primitive type Session.get(class, <primitive>) is invalid (we're using JDK 1.42, I'm not sure what happens if jdk5 is set). If PRIMITIVES were exposed via a getter it would actually be much cleaner since it could be:
Code:
<#if c2j.isPrimitive(c2j.getJavaTypeName(clazz.identifierProperty, jdk5))>
<#assign safeId = "new " + c2j.getPrimitives().get( c2j.getJavaTypeName(clazz.identifierProperty, jdk5)) + "(id)">
<#else>
<#assign safeId = "id">
</#endif>
Then the value of safeId is passed into the 2nd param of Session.get() instead of id. Would this seem to be a legitimate fix for this issue? It seems to do the trick here, anyway...
Thanks!