升级到SpringBoot2.0以上版本后,配置Jpa多数据源遇到的问题

在升级SpringBoot至2.0及以上版本后,配置Jpa多数据源遇到了挑战,主要包括SQLiteDialectIdentityColumnSupport构造方法的调整、JpaConfiguration类的适配问题,以及SessionFactoryImpl的getAllClassMetadata()方法的弃用。通过改造相关代码,成功解决了这些问题。
摘要由CSDN通过智能技术生成

最近在整理一个SpringBoot+有多数据源的Demo,方言有SQLite和Mysql,SpringBoot使用的版本是<version>1.5.10.RELEASE</version>,想升级2.0以上的版本,于是出现了一系列版本冲突问题,小问题就不贴了,只贴处理最久的一个Jpa多数据源配置问题,在网上记录了该问题解决方法的鲜有几人。

<version>1.5.10.RELEASE</version>原代码

DataSourceConfiguration 类

package com.xxxx.core.sqlite.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.sqlite.SQLiteDataSource;

/**
 * 配置sqlite数据库的DataSource
 * 
 */
@Configuration
public class DataSourceConfiguration {
   
	
	@Value("${sqlite.dbName}")
	private String dbName;

	@Bean(destroyMethod = "", name = "EmbeddeddataSource")
	public DataSource dataSource() {
   
		DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
		dataSourceBuilder.driverClassName("org.sqlite.JDBC");
		dataSourceBuilder.url("jdbc:sqlite:" + dbName);
		dataSourceBuilder.type(SQLiteDataSource.class);
		return dataSourceBuilder.build();
	}

}

JpaConfiguration 类

package com.xxxx.core.sqlite.config;

import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

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.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableJpaRepositories(basePackages = "com.xxxx.core.repository", transactionManagerRef = "jpaTransactionManager", entityManagerFactoryRef = "localContainerEntityManagerFactoryBean")
@EnableTransactionManagement
public class JpaConfiguration {
   

	@Resource
	private JpaProperties jpaProperties;

	@Autowired
	@Bean
	public JpaTransactionManager jpaTransactionManager(@Qualifier(value = "EmbeddeddataSource") DataSource dataSource, EntityManagerFactory entityManagerFactory) {
   
		JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
		jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
		jpaTransactionManager.setDataSource(dataSource);

		return jpaTransactionManager;
	}

	@Autowired
	@Bean
	LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean(@Qualifier(value = "EmbeddeddataSource") DataSource dataSource, EntityManagerFactoryBuilder builder) {
   
		return builder.dataSource(dataSource).packages("com.xxxx.core.model").properties(getVendorProperties(dataSource)).build();
	}

	private Map<String, String> getVendorProperties(DataSource dataSource) {
   
		return jpaProperties.getHibernateProperties(dataSource);
	}

}

ModelMetaData 类

package com.xxxx.core.sqlite.config;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManagerFactory;

import org.hibernate.SessionFactory;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 构建一个存放表名和model实体class的对应关系,如member_entity:MemberEntity.class
 *
 */
@Configuration
@AutoConfigureAfter(JpaConfiguration.class)
public class ModelMetaData {
   

	@Bean(name = "metaMap")
	public Map<String, Class> metaMap(EntityManagerFactory factory) throws ClassNotFoundException {
   
		if (factory.unwrap(SessionFactory.class) == null) {
   
			throw new NullPointerException("factory is not a hibernate factory");
		}
		SessionFactory sessionFactory = factory.unwrap(SessionFactory.class);
		Map<String, ClassMetadata> metaMap = sessionFactory.getAllClassMetadata();
		Map<String, Class> map = new HashMap<>(metaMap.size());
		for (String key : metaMap.keySet()) {
   
			AbstractEntityPersister classMetadata = (AbstractEntityPersister) metaMap.get
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值