简体   繁体   English

org.postgresql.util.PSQLException:错误:列“标签”是 jsonb 类型,但表达式的类型是字符变化

[英]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.

相关问题 org.postgresql.util.PSQLException:错误:WHERE的参数必须为布尔型,而不能改变字符类型 - org.postgresql.util.PSQLException: ERROR: argument of WHERE must be type boolean, not type character varying org.postgresql.util.PSQLException: 错误: 类型字符变化的值太长 (255) - org.postgresql.util.PSQLException: ERROR: value too long for type character varying(255) Caused by: org.postgresql.util.PSQLException: ERROR: value too long for type character varying(255) 无缘无故 - Caused by: org.postgresql.util.PSQLException: ERROR: value too long for type character varying(255) with no reason org.postgresql.util.PSQLException:错误:“ geo_detail”列的类型为point,但是表达式的类型为bytea? - org.postgresql.util.PSQLException: ERROR: column “geo_detail” is of type point but expression is of type bytea any solution please? org.postgresql.util.PSQLException:错误:列的类型为日期,但表达式的类型为 integer:无法使用 Z93F725A07423FE1C889F448B33D21F4 将日期写入 postgres - org.postgresql.util.PSQLException: ERROR: column is of type date but expression is of type integer: cannot write date into postgres using java Caused by: org.postgresql.util.PSQLException: ERROR: column "low_range" is of type double precision 但 expression 是类型 bytea - Caused by: org.postgresql.util.PSQLException: ERROR: column "low_range" is of type double precision but expression is of type bytea org.postgresql.util.PSQLException:BigDecimal 类型的值错误:{1} 与 JPA - org.postgresql.util.PSQLException: Bad value for type BigDecimal : {1} with JPA 由以下原因引起:org.postgresql.util.PSQLException:错误:类型“枚举”不存在 - Caused by: org.postgresql.util.PSQLException: ERROR: type “enum” does not exist org.postgresql.util.PSQLException:错误:inet类型的输入语法无效: - org.postgresql.util.PSQLException: ERROR: invalid input syntax for type inet: org.postgresql.util.PSQLException:错误:列 systementi0_.id 不存在 - Hibernate、PostgreSql - org.postgresql.util.PSQLException: Error: column systementi0_.id does not exist - Hibernate, PostgreSql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM