我想知道什么是使DbUnit与MySQL的JSON(B)类型列一起工作的最佳方法?我们确实有这样的列,每当我试图通过@DatabaseSetup
从XML文件中提供测试数据时,我得到NoSuchColumnException,这阻止我与实体或repos处理JSON进行任何合理的集成测试:
org.dbunit.dataset.NoSuchColumnException: assistant_event.AEV_CONTEXT - (Non-uppercase input column: aev_context) in ColumnNameToIndexes cache map. Note that the map's column names are NOT case sensitive.
我知道这是因为我的AEV_CONTEXT
专栏没有得到认可,因为它是一个JSON专栏:
@Type(type = "json")
@Column(name = "aev_context", columnDefinition = "json")
private Context context;
但是,我在尝试解决它时遇到了困难。而且,更奇怪的是,我无法在这里找到这样的解决方法!事实上,我不知道事件是否更多是Hibernate或DbUnit的事情。
到目前为止,我真的只有这个问题吗?任何建议将不胜感激!
哦,如果你想知道,那就是我如何获得Hibernate的JSON支持:
https://vladmihalcea.com/how-to-map-json-objects-using-generic-hibernate-types/
一个可能的解决方案是定义您自己的DataTypeFactory并使用它。我这样做是为了支持PostgreSQL JSONB数据类型,可以为MySQL做类似的事情。
class NewPostgresqlDataTypeFactory : PostgresqlDataTypeFactory() {
override fun createDataType(sqlType: Int, sqlTypeName: String?): DataType {
return when (sqlTypeName) {
"jsonb" -> return JsonbDataType()
else -> super.createDataType(sqlType, sqlTypeName)
}
}
class JsonbDataType : AbstractDataType("jsonb", Types.OTHER, String::class.java, false) {
override fun typeCast(obj: Any?): Any {
return obj.toString()
}
override fun getSqlValue(column: Int, resultSet: ResultSet): Any {
return resultSet.getString(column)
}
override fun setSqlValue(value: Any?, column: Int, statement: PreparedStatement) {
val jsonObj = PGobject()
jsonObj.type = "json"
jsonObj.value = value?.toString()
statement.setObject(column, jsonObj)
}
}
}
如果有人需要它,这是用于PostgreSQL JSONB数据类型的Java版本的@Aleksi's solution:
import org.dbunit.dataset.datatype.AbstractDataType;
import org.dbunit.dataset.datatype.DataType;
import org.dbunit.dataset.datatype.DataTypeException;
import org.dbunit.dataset.datatype.TypeCastException;
import org.dbunit.ext.postgresql.PostgresqlDataTypeFactory;
import org.postgresql.util.PGobject;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
public class NewPostgresqlDataTypeFactory extends PostgresqlDataTypeFactory {
@Override
public DataType createDataType(int sqlType, String sqlTypeName) throws DataTypeException {
if (sqlTypeName.equals("jsonb")) {
return new JsonbDataType();
} else {
return super.createDataType(sqlType, sqlTypeName);
}
}
public static class JsonbDataType extends AbstractDataType {
public JsonbDataType() {
super("jsonb", Types.OTHER, String.class, false);
}
@Override
public Object typeCast(Object obj) throws TypeCastException {
return obj.toString();
}
@Override
public Object getSqlValue(int column, ResultSet resultSet) throws SQLException, TypeCastException {
return resultSet.getString(column);
}
@Override
public void setSqlValue(Object value,
int column,
PreparedStatement statement) throws SQLException, TypeCastException {
final PGobject jsonObj = new PGobject();
jsonObj.setType("json");
jsonObj.setValue(value == null ? null : value.toString());
statement.setObject(column, jsonObj);
}
}
}