dbunit和json列类型支持

rqmkfv5c  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(312)

我想知道让dbunit与mysql的json(b)类型列一起工作的最佳方法是什么?每当我试图通过 @DatabaseSetup 我得到nosuchcolumnexception,它阻止我对处理json的实体或repos进行任何合理的集成测试:

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;

然而,我在努力解决这个问题时遇到了困难。更奇怪的是,我在这里也找不到这样的解决办法!事实上,我不知道这是休眠还是dbunit。
到目前为止真的只有我有这个问题吗?任何建议都将不胜感激!
哦,如果你想知道的话,我就是这样得到了对hibernate的json支持的:
https://vladmihalcea.com/how-to-map-json-objects-using-generic-hibernate-types/

zbsbpyhn

zbsbpyhn1#

如果有人需要,这是@aleksi的postgresql jsonb数据类型解决方案的java版本:

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);
        }
    }
}
pb3skfrl

pb3skfrl2#

一个可能的解决方案是定义自己的datatypefactory并使用它。我这样做是为了支持postgresqljsonb数据类型,类似的东西也可以用于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)
        }
    }
}

相关问题