|
|
@@ -14,10 +14,14 @@ import java.sql.SQLException;
|
|
|
|
|
|
@MappedJdbcTypes(JdbcType.OTHER)
|
|
|
@MappedTypes(Object.class)
|
|
|
-public class JsonbTypeHandler extends BaseTypeHandler<Object> {
|
|
|
+public class JsonbTypeHandler<T> extends BaseTypeHandler<Object> {
|
|
|
|
|
|
private static final ObjectMapper mapper = new ObjectMapper();
|
|
|
+ private final Class<T> type;
|
|
|
|
|
|
+ public JsonbTypeHandler(Class<T> type) {
|
|
|
+ this.type = type;
|
|
|
+ }
|
|
|
@Override
|
|
|
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
|
|
|
try {
|
|
|
@@ -31,26 +35,26 @@ public class JsonbTypeHandler extends BaseTypeHandler<Object> {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
+ public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
|
|
return parseJson(rs.getString(columnName));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
+ public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
|
|
return parseJson(rs.getString(columnIndex));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
+ public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
|
|
return parseJson(cs.getString(columnIndex));
|
|
|
}
|
|
|
|
|
|
- private Object parseJson(String json) throws SQLException {
|
|
|
+ private T parseJson(String json) throws SQLException {
|
|
|
if (json == null) {
|
|
|
return null;
|
|
|
}
|
|
|
try {
|
|
|
- return mapper.readValue(json, Object.class);
|
|
|
+ return mapper.readValue(json, type);
|
|
|
} catch (JsonProcessingException e) {
|
|
|
throw new SQLException("Error parsing JSONB string", e);
|
|
|
}
|