ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Vadim Grinshpun <va...@ll.mit.edu>
Subject "can't infer the SQL type" of a bean property whose type is a generic parameter.
Date Fri, 18 Jan 2008 16:24:06 GMT
Hi all,

I just ran into an interesting problem, and I'm curious whether I'm 
doing something wrong, or if it's a known limitation or bug in iBATIS 
(or its interaction with the JDBC driver?). I'm using iBATIS 2.3.0 with 
a PostgreSQL's jdbc driver 8.2-504.

Let's say I have the following classes:

// wrapper that attaches an attribute to things; a poor-man's mix-in class
class Wrapper<T>{
    String wrapperAttribute;
    T value;
    String getAttribute() { return wrapperAttribute;}
    T getValue() { return value; };
     // remaining bean accessors are defined but not shown
     // ...
}

// a class that uses the  wrapper:
class FooBean{
   Wrapper<String> wrappedString;
   Wrapper<Date> wrappedDate;
   Wrapper<SomeEnum> wrappedEnum; // Enum and its TypeHandlerCallback 
are defined elsewhere, not shown
   // the obvious get/set accessors are defined below, not shown
   // ...
}
// end code

FooBean is then passed as a parameter to an insert statement, which 
looks, roughly, along the lines of:

 <insert ... >
      INSERT INTO mytable ( string_field, date_field, enum_field )
      VALUES( #wrappedString.value#,
                      #wrappedDate.value#,
                      #wrappedEnum.value# )
 </insert>
 
When trying to run the code, the #wrappedString.value# part of the 
statement works just fine, but for the #wrappedDate.value# I  get an error:
    "PSQLException: Can't infer the SQL type to use for an instance of 
java.util.Date. Use setObject() with an explicitly Types value to 
specify the type to use."

Seeing this, I tried adding the jdbc type to the parameter map, as shown:
                      #wrappedDate.value:TIMESTAMP#
This produced the same exact error.

Then I tried adding a wrapper method,"Date getWrappedDateValue" which 
simply calls getWrappedDate().getValue(), and thus returns a Date 
object, but then I got the same error for the wrappedEnum...

Can anyone explain this? Is there a way to get this to work properly? I 
first thought that this has to do with Java's generic not preserving 
enough information for this to work, but as the error message clearly 
shows, the type info is retrievable...
Thanks for any insight!

-Vadim


Mime
View raw message