Clintion,
Thanks for your response. I have resolved the issue by just changing my
ResultSet getTimestamp/getDate to getObject since I can get the date string
just by calling toString on the getObject response. The problem I was
having was not knowing whether I could call getTimestamp or getDate because
I did not have any information in the getResult methods (of TypeHandler).
So I was making an assumption that getTimestamp would work for both sql
types Date/Timestamp. But my code was erroring out. So changing the
getDate/getTimestamp to getObject...I am able to extract the String from
getObject and parse it (using a format) into a DateTime object. This
problem is resolved.
But I still cannot create TypeHandlers for (interfaces/abstract classes)
that can apply to the implementations of those classes. So...like my Jira
entry...we have our own pre-JDK1.5 class called TypeSafeEnumeration to
represent our Enums. We have to create an instance of this for each Enum we
use and therefore we have to create a handler for each instance. If the
TypeHandler getResult method passed in the additional iBatis configuration
information like javaType, I could create one TypeHandler for the superclass
TypeSafeEnumeration and use the javaType parameter to cast returned Object
to the correct type.
For example, this is what it could look like:
public class AbstractTypeSafeEnumerationTypeHandler implements
NewTypeHandler {
/**
* This method overrides the default setParameter method in TypeHandler.
*
* @param ps param
* @param i param
* @param parameter param
* @param jdbcType param
*
* @throws SQLException can be thrown
*/
public void setParameter(PreparedStatement ps, int i, Object parameter,
String jdbcType)
throws SQLException {
if(parameter == null) {
JdbcType type =
(JdbcType)JdbcType.getTypeByDisplayValue(jdbcType, JdbcType.class);
int sqlType = Integer.parseInt(type.getValue());
ps.setNull(i, sqlType);
}
else {
AbstractTypeSafeEnumeration enum =
(AbstractTypeSafeEnumeration)parameter;
ps.setString(i, enum.getValue());
}
}
private AbstractTypeSafeEnumeration getEnum(String value, String
javaType) {
Class clazz = null;
try {
clazz = Class.forName(javaType);
}
catch (ClassNotFoundException e) {
throw new RuntimeException("Invalid javaType - This class does
not exist: -> " + javaType, e);
}
return AbstractTypeSafeEnumeration.getTypeByValue(value, clazz);
}
/**
* This method overrides the default getResult method in TypeHandler.
*
* @param rs param
* @param columnName param
*
* @return returned
*
* @throws SQLException can be thrown
*/
public Object getResult(ResultSet rs, String columnName, String
javaType)
throws SQLException {
String result = rs.getString(columnName);
return this.getEnum(result, javaType);
}
/**
* This method overrides the default getResult method in TypeHandler.
*
* @param rs param
* @param columnIndex param
*
* @return returned
*
* @throws SQLException can be thrown
*/
public Object getResult(ResultSet rs, int columnIndex, String javaType)
throws SQLException {
String result = rs.getString(columnIndex);
return this.getEnum(result, javaType);
}
/**
* This method overrides the default getResult method in TypeHandler.
*
* @param cs param
* @param columnIndex param
*
* @return returned
*
* @throws SQLException can be thrown
*/
public Object getResult(CallableStatement cs, int columnIndex, String
javaType)
throws SQLException {
String result = cs.getString(columnIndex);
return this.getEnum(result, javaType);
}
/**
* This method overrides the default equals method in TypeHandler.
*
* @param object param
* @param dbVal param
*
* @return returned
*/
public boolean equals(Object object, String dbVal) {
return
StringHelper.equals(((AbstractTypeSafeEnumeration)object).getValue(),
dbVal);
}
/**
* This method overrides the default valueOf method in TypeHandler.
*
* @param dbVal param
*
* @return returned
*/
public Object valueOf(String dbVal) {
return dbVal;
}
}
--
View this message in context:
http://www.nabble.com/jdbcType-javaType-accessibility-in-TypeHandler-tf2130612.html#a5928370
Sent from the iBATIS - User - Java forum at Nabble.com.