[英]org.postgresql.util.PSQLException: ERROR: column "tags" is of type jsonb but expression is of type character varying
After I changed the PostgreSQL 13 database columns tags
to jsonb, throw error when execute sql:在我将 PostgreSQL 13 数据库列tags
更改为 jsonb 后,执行 sql 时抛出错误:
Caused by: org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: column "tags" is of type jsonb but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 519
### The error may exist in class path resource [mybatis/mapper/dolphin/RssSubSourceMapper.xml]
### The error may involve com.dolphin.soa.post.dao.RssSubSourceMapper.updateByPrimaryKeySelective-Inline
### The error occurred while setting parameters
### SQL: UPDATE rss_sub_source SET sub_url = ?, created_time = ?, updated_time = ?, sub_status = ?, rss_type = ?, standard_type = ?, standard_version = ?, cron = ?, trigger_count = ?, next_trigger_time = ?, sub_name = ?, last_trigger_time = ?, intro = ?, failed_count = ?, frequency_month = ?, reputation = ?, rep_latest_refresh_time = ?, scrapy_take_time = ?, censor_status = ?, etag = ?, last_modified = ?, editor_pick = ?, fav_icon_url = ?, dynamic_interval = ?, creator = ?, tags = ?, article_count = ?, article_count_latest_refresh_time = ?, comment_rss = ?, part_output = ? WHERE id = ?
### Cause: org.postgresql.util.PSQLException: ERROR: column "tags" is of type jsonb but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 519
; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: column "tags" is of type jsonb but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 519
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239) ~[spring-jdbc-5.3.19.jar!/:5.3.19]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70) ~[spring-jdbc-5.3.19.jar!/:5.3.19]
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:91) ~[mybatis-spring-2.0.6.jar!/:2.0.6]
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441) ~[mybatis-spring-2.0.6.jar!/:2.0.6]
at com.sun.proxy.$Proxy135.update(Unknown Source) ~[?:?]
at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:288) ~[mybatis-spring-2.0.6.jar!/:2.0.6]
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:67) ~[mybatis-3.5.6.jar!/:3.5.6]
at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:152) ~[mybatis-3.5.6.jar!/:3.5.6]
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85) ~[mybatis-3.5.6.jar!/:3.5.6]
at com.sun.proxy.$Proxy152.updateByPrimaryKeySelective(Unknown Source) ~[?:?]
what should I do to handle the jsonb in spring boot application when using mybatis?使用mybatis时spring boot应用中的jsonb怎么处理?
Finally add the JsonTypeHandler
fixed this problem:最后添加JsonTypeHandler
修复了这个问题:
package misc.config.mybatis;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.postgresql.util.PGobject;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@MappedTypes({Object.class})
public class JsonTypeHandler extends BaseTypeHandler<Object> {
private Class<Object> clazz;
public JsonTypeHandler(Class<Object> clazz) {
this.clazz = clazz;
}
public JsonTypeHandler() {
}
private static final PGobject jsonObject = new PGobject();
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
jsonObject.setType("json");
jsonObject.setValue(JSON.toJSONString(parameter));
ps.setObject(i, jsonObject);
}
@Override
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
return JSON.parseObject(rs.getString(columnName), clazz);
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return JSON.parseObject(rs.getString(columnIndex), clazz);
}
@Override
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return JSON.parseObject(cs.getString(columnIndex), clazz);
}
}
in the mybatis mapper xml, specify the custom JsonTypeHandler like this:在 mybatis 映射器 xml 中,像这样指定自定义 JsonTypeHandler:
<result column="tags" jdbcType="OTHER" property="tags" typeHandler="misc.config.mybatis.JsonTypeHandler" />
if using mybatis code auto generator, add code generator config like this:如果使用 mybatis 代码自动生成器,添加代码生成器配置如下:
<table tableName="rss_sub_source"
enableCountByExample="true"
enableUpdateByExample="true"
enableDeleteByExample="true"
enableSelectByExample="true"
selectByExampleQueryId="true">
<generatedKey column="ID" sqlStatement="JDBC" identity="true" />
<columnOverride column="tags" jdbcType="OTHER" typeHandler="misc.config.mybatis.JsonTypeHandler"/>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.