之前项目中使用的数据源是Greeplum(GP),现在在本地windows安装了pg代替gp,其他运行都还好。就一个sql时出现了一个异常:
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)
因为实现的是:jpa的方式,不存在Hibernate的那一套mapping。。。
最后找了一通,是因为pg配置的数据源的问题。。现更改为:
添加配置类
package com.trs.idap.config;
import org.hibernate.dialect.PostgreSQL94Dialect;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import org.hibernate.type.descriptor.sql.VarcharTypeDescriptor;
import java.sql.Types;
/**
* @创建人:Young
* @时 间: 2019/4/12
* @描 述: TODO
*/
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");
}
}
将该类应用到数据库应用中
package com.trs.idap.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryGreenplum", //实体管理引用
transactionManagerRef = "transactionManagerGreenplum", //事务管理引用
basePackages = {"com.trs.idap.repository.greenplum"}) //设置 myagenDataSource应用到的包
public class GreenPlumConfiguration {
@Autowired
@Qualifier(value = "GPSource")
private DataSource gpDataSource;
/**
* 注入JPA配置实体
*/
@Autowired
private JpaProperties jpaProperties;
/**
* 通过调用JPA配置实体中的解析方法,解析datasource中各属性的值
* @param dataSource 数据源
* @return 本数据源中各参数
* Map中设值分别为:
* hibernate-dialect 方言
* hibernate.hbm2ddl.auto DDL执行策略
* hibernate.physical_naming_strategy 命名策略
*
*这些和不同类型数据库密切相关的属性设置,不能设置在application.properties中,所以需要再不同的数据源中具体设置,赋值给JpaProperties
*/
private Map<String,String> getVendorProperties(DataSource dataSource){
jpaProperties.setDatabase(Database.POSTGRESQL);
Map<String,String> map = new HashMap<>();
map.put("hibernate.dialect","com.trs.idap.config.PgDialect");
// map.put("hibernate.hbm2ddl.auto","update");
// map.put("hibernate.physical_naming_strategy","org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
jpaProperties.setProperties(map);
return jpaProperties.getHibernateProperties(dataSource);
}
/**
* 配置EntityManagerFactory实体
* @param builder
* @return 实体管理工厂
* packages 扫描@Entity注释的软件包名称
* persistenceUnit 持久性单元的名称。 如果只建立一个EntityManagerFactory,你可以省略这个,但是如果在同一个应用程序中有多个,你应该给它们不同的名字
* properties 标准JPA或供应商特定配置的通用属性。 这些属性覆盖构造函数中提供的任何值。
*
*/
@Bean(name = "entityManagerFactoryGreenplum")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryGreenplum(EntityManagerFactoryBuilder builder){
return builder
.dataSource(gpDataSource)
.properties(getVendorProperties(gpDataSource))
.packages(new String[]{"com.trs.idap.domain.entity.greenplum"})
.persistenceUnit("GreenPlumDiscountPersistenceUnit")
.build();
}
/**
* 配置EntityManager实体
* @param builder
* @return 实体管理器
*/
@Bean(name = "entityManagerGeenplum")
public EntityManager entityManager(EntityManagerFactoryBuilder builder){
return entityManagerFactoryGreenplum(builder).getObject().createEntityManager();
}
/**
* 配置事务transactionManager
* @param builder
* @return 事务管理器
*/
@Bean(name = "transactionManagerGreenplum")
public PlatformTransactionManager transactionManagerGreenplum(EntityManagerFactoryBuilder builder){
return new JpaTransactionManager(entityManagerFactoryGreenplum(builder).getObject());
}
}
此时配置pg的方言,重启验证。。。