I'm using Enums without difficulty. I've implemented a
TypeHandlerCallback to accommodate the set and get in the following
manner:
public
Object getResult(ResultGetter getter) throws SQLException {String value = getter.getString();
if (getter.wasNull())
return null;
return YourEnum.valueOf(value);
}
public void setParameter(ParameterSetter setter, Object parameter) throws SQLException { if (parameter == null) {
setter.setNull(Types.
VARCHAR);}
else {YourEnum en = (YourEnum) parameter;
setter.setString(en.toString());
}
}
From: Tom Duffey <[EMAIL PROTECTED]> [mailto:Tom Duffey <[EMAIL PROTECTED]>]
Sent: Monday, October 16, 2006 4:48 PM
To: [email protected]
Subject: Problem w/enum and TypeHandlerCallback
I'm having a problem with Java 5 Enums and iBATIS
TypeHandlerCallbacks. I've read the wiki article and it doesn't help
with my particular issue. Retrieving objects is working fine but
inserting/updating them is throwing this NPE:
java.lang.NullPointerException
at com.ibatis.sqlmap.engine.type.UnknownTypeHandler.setParameter
(UnknownTypeHandler.java:72)
at
com.ibatis.sqlmap.engine.mapping.parameter.BasicParameterMap.setParamete
r(BasicParameterMap.java:165)
at
com.ibatis.sqlmap.engine.mapping.parameter.BasicParameterMap.setParamete
rs(BasicParameterMap.java:125)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate
(SqlExecutor.java:76)
at
com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.sqlExecuteUp
date(GeneralStatement.java:200)
at
com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeUpdat
e(GeneralStatement.java:78)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert
(SqlMapExecutorDelegate.java:446)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert
(SqlMapSessionImpl.java:82)
at org.springframework.orm.ibatis.SqlMapClientTemplate
$9.doInSqlMapClient(SqlMapClientTemplate.java:358)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute
(SqlMapClientTemplate.java:188)
at org.springframework.orm.ibatis.SqlMapClientTemplate.insert
(SqlMapClientTemplate.java:356)
...
Here's my basic setup:
public interface Tenant {
public enum Type { FOO, BAR }
public Type getType();
public void setType(Type type);
...
}
public class TenantImpl implements Tenant {
private Type type;
public Type getType() { return type; }
public void setType(Type type) { this.type = type; }
...
}
My type handler for Tenant$Type looks a lot like the examples in the
wiki article. The types are mapped in my SqlMapConfig:
I'm messing around with UnknownTypeHandler.setParameter() and am very
confused. I checked the incoming value of parameter.getClass() and
it is correct, i.e., foo.bar.Tenant$Type. usingJavaPre5 is set
(Which is confusing by itself because I am using Java5) so
getBaseClass() is called and the new searchClass is foo.bar.Tenant.
I do not have or want a type handler for foo.bar.Tenant. Can anyone
help?
Tom
