i have a pojo with some field of type Set<String>
.
I want to persist them in the db as json, so i create a custom typeHandler, but when i try to persist i get the error: There was no TypeHandler found ....
testSave(it.infora.suap.service.MailMessageServiceTest): nested exception is org.apache.ibatis.executor.ExecutorException: There was no TypeHandler found for parameter recipients_to of statement it.infora.suap.persistence.MailMessageMapper.insertEmailMessage
the parameter recipients_to in a Set<String>
this is my custom typeHandler class:
public class SetTypeHandler implements TypeHandler<Set<String>>{
@Override
public void setParameter(PreparedStatement ps, int columnIndex, Set<String> parameter, JdbcType jt) throws SQLException {
ps.setString(columnIndex, serializeToJson(parameter));
}
@Override
public Set<String> getResult(ResultSet rs, String columnName) throws SQLException {
return deserializeFromJson(rs.getString(columnName));
}
@Override
public Set<String> getResult(ResultSet rs, int columnIndex) throws SQLException {
return deserializeFromJson(rs.getString(columnIndex));
}
@Override
public Set<String> getResult(CallableStatement cs, int columnIndex) throws SQLException {
return deserializeFromJson(cs.getString(columnIndex));
}
private String serializeToJson(Set<String> parameter){
Gson gson = new Gson();
return gson.toJson(parameter);
}
private Set<String> deserializeFromJson(String value){
Gson gson = new Gson();
Type collectionType = new TypeToken<Set<String>>(){}.getType();
Set<String> result = gson.fromJson(value, collectionType);
return result;
}
}
what's wrong? I'm using annotated mapper interface instead of mapper.xml
Thanks
Andrea