emacarron / mybatis

Automatically exported from code.google.com/p/mybatis
0 stars 0 forks source link

EnumTypeHandler for postgresql enums doesn't work #103

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using?
MyBatis 3.0.2

Please describe the problem.  Unit tests are best!
EnumTypeHandler for enums on postgresql database doesn't work

What is the expected output? What do you see instead?

Caused by: org.postgresql.util.PSQLException: ERROR: column "classtype" is of 
type classtype but expression is of type character varying
  Hint: You will need to rewrite or cast the expression. 

Please provide any additional information below.

Updated EnumTypeHandler with following code fixes the issue. Can this be 
included in base distribution?

  public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
      ps.setObject(i, parameter.toString(), java.sql.Types.OTHER);
  }

Original issue reported on code.google.com by megamanf...@gmail.com on 14 Sep 2010 at 8:41

GoogleCodeExporter commented 9 years ago
Ah, thanks for tihs... I wasn't sure what the underlying problem was, my 
workaround was to write the sql queries like this:

'${enumVar}' preventing it from showing up as a param in a PreparedStatement, 
as opposed to #{enumVar}

Original comment by alex.she...@gmail.com on 22 Sep 2010 at 12:48

GoogleCodeExporter commented 9 years ago
I think an acceptable work-around for this is to define you're own 
EnumTypeHandler that does this.
I am not against using OTHER as the JDBC type, but the actual JDBC type will 
probably vary depending on the database. So using OTHER for everybody may cause 
problems.

Original comment by christia...@ircm.qc.ca on 17 Dec 2010 at 1:33

GoogleCodeExporter commented 9 years ago

Original comment by christia...@ircm.qc.ca on 17 Dec 2010 at 1:34

GoogleCodeExporter commented 9 years ago
What about introducing JdbcType.ENUM and in then setNonNullParameter only using 
java.sql.Types.OTHER when jdbcType == JdbcType.ENUM ?

Original comment by herman.b...@gmail.com on 13 Jan 2011 at 4:45

GoogleCodeExporter commented 9 years ago
Using OTHER as JDBC type does not work for some databases.
I've changed the code so that VARCHAR is the default, but if you specify OTHER 
as the JDBC type, it will be set correctly.

Can you check if it works?

The new code for EnumTypeHandler is
  public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
    if (jdbcType == null) {
        ps.setString(i, parameter.toString());
    } else {
        ps.setObject(i, parameter.toString(), jdbcType.TYPE_CODE);
    }
  }

Original comment by christia...@ircm.qc.ca on 26 Jan 2011 at 3:15

GoogleCodeExporter commented 9 years ago
Since I didn't have any feedback since the last 2 months, I'll consider this 
bug to be fixed.

Original comment by christia...@ircm.qc.ca on 15 Apr 2011 at 12:52

GoogleCodeExporter commented 9 years ago

Original comment by eduardo.macarron on 3 Mar 2012 at 9:11

GoogleCodeExporter commented 9 years ago

Original comment by eduardo.macarron on 3 Mar 2012 at 9:13