欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JPA之"org.hibernate.MappingExceptception: No Dialect mapping for JDBC type: 1111"问题

程序员文章站 2022-04-13 17:01:51
...

问题背景

JPA连接PGSQL,有个表一执行基本CRUD操作就报错

Hibernate: select * from question_topics where state= ? 
? 
sqlTypeDescriptor.getSqlType()-e()->12
12
org.springframework.orm.jpa.JpaSystemExceException: No Dialect mapping for JDBC type: 1111; nested exception is is org.hibernate.MappingExceptception: No Dialect mapping for JDBC type: 1111
    at at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernbernateAccessException(on(HibernateJpaDialect.java:314)
    a)
    at at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceExceptionIfPossible(le(HibernateJpaDialect.java:225)
    a)
    at at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceExceptionIfPossible(le(AbstractEntityManagerFactoryBean.java:527)
    a)
    at at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceExceptionIfPossible(le(ChainedPersistenceExceptionTranslator.java:61)
    a)
    at at org.springframework.dao.support.DataAccessUtils.translateIfNeIfNecessary(ry(DataAccessUtils.java:242)
    a)
    at at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(Per(ke(PersistenceExceptionTranslationInterceptor.java:153)
    a)
    at at org.springframework.aop.framework.ReflectiveMethodInvocation.proceedceed(ed(ReflectiveMethodInvocation.java:185)
    a)
    at at org.springframework.data.jpa.repository.support.CrudMethodMetdMetadataPostProcessor$or$CrudMethodMetadataPopulatingMethodInterceptor.invoke(Cru(ke(CrudMethodMetadataPostProcessor.java:135)
    a)
    at at org.springframework.aop.framework.ReflectiveMethodInvocation.proceedceed(ed(ReflectiveMethodInvocation.java:185)
    a)
    at at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(Exp(ke(ExposeInvocationInterceptor.java:92)
    a)
    at at org.springframework.aop.framework.ReflectiveMethodInvocation.proceedceed(ed(ReflectiveMethodInvocation.java:185)
    a)
    at at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(Sur(ke(SurroundingTransactionDetectorMethodInterceptor.java:61)
    a)
    at at org.springframework.aop.framework.ReflectiveMethodInvocation.proceedceed(ed(ReflectiveMethodInvocation.java:185)
    a)
    at at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(Jdk(ke(JdkDynamicAopProxy.java:212)
    a)
    at at com.sun.proxy.$xy.$Proxy

解决方案

自己手撸一个 PgDialect 来引用,不用默认的的PostgreSQL94Dialect

import org.hibernate.dialect.PostgreSQL94Dialect;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import org.hibernate.type.descriptor.sql.VarcharTypeDescriptor;

import java.sql.Types;

public class PgDialect extends PostgreSQL94Dialect
{
    @Override
    public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor)
    {
        switch (sqlTypeDescriptor.getSqlType())
        {
        case Types.CLOB:
            return VarcharTypeDescriptor.INSTANCE;
        case Types.BLOB:
            return VarcharTypeDescriptor.INSTANCE;
        case 1111://1111应该是pgsql的json
            return VarcharTypeDescriptor.INSTANCE;
        }
        return super.remapSqlTypeDescriptor(sqlTypeDescriptor);
    }
    public PgDialect() {
        super();
        registerHibernateType(1111, "string");
    }
}

application.yml文件配置如下

spring:
    http:
      encoding:
        force: true
        charset: UTF-8
    application:
        name: tcbj-yytsg-checkcollectsys
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: org.postgresql.Driver
        #driver-class-name: org.microservice.tcbj.yytsg.checkcollectsys.config.PgDriver
        platform: postgresql
        url: jdbc:postgresql://xxxxx:5432/xxxxx?stringtype=unspecified&useUnicode=true&characterEncoding=utf-8&useSSL=false
        username: ******
        password: ******
        initialSize: 5
        minIdle: 5
        maxActive: 20
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        filters: stat,wall,log4j
        logSlowSql: true
    jpa:
        properties:
            hibernate:
                temp:
                    use_jdbc_metadata_defaults: false
                hbm2ddl:
                    auto: update
        show-sql: true
        naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
        #核心定义再这里
        database-platform: org.microservice.tcbj.yytsg.checkcollectsys.config.PgDialect