Springboot 学习之 JPA + Hibernate 双实现

前言

有时,我们希望在项目中 同时支持 HIbernate JPAHibernate 自有 的 ORM 实现。

事务配置

数据源 及 Hibernate 属性配置

	package com.zxguan.hibernateAndJpa.config.properties;
	
	import org.springframework.beans.factory.annotation.Value;
	import org.springframework.context.annotation.Bean;
	import org.springframework.context.annotation.Configuration;
	import org.springframework.jdbc.datasource.DriverManagerDataSource;
	
	import javax.sql.DataSource;
	
	@Configuration
	public class DatasourceProperties {
	
	    @Value("${datasource.url}")
	    private String url;
	
	    @Value("${datasource.driver-class-name}")
	    private String driverClassName;
	
	    @Value("${datasource.username}")
	    private String username;
	
	    @Value("${datasource.password}")
	    private String password;
	
	    @Bean
	    public DataSource dataSource() {
	        DriverManagerDataSource dataSource = new DriverManagerDataSource();
	        dataSource.setUrl(url);
	        dataSource.setDriverClassName(driverClassName);
	        dataSource.setUsername(username);
	        dataSource.setPassword(password);
	        return dataSource;
	    }
	}
	package com.zxguan.hibernateAndJpa.config.properties;
	
	import org.springframework.beans.factory.annotation.Value;
	import org.springframework.context.annotation.Bean;
	import org.springframework.context.annotation.Configuration;
	
	import java.util.Properties;
	
	@Configuration
	public class HibernateProperties {
	
	    public static final String BASE_PACKAGES = "com";
	
	    @Value("${hibernate.dialect}")
	    private String dialect;
	
	    @Value("${hibernate.show_sql}")
	    private String showSql;
	
	    @Value("${hibernate.format_sql}")
	    private String formatSql;
	
	    @Value("${hibernate.hbm2ddl.auto}")
	    private String hbm2ddlAuto;
	
	    @Value("${hibernate.current_session_context_class}")
	    private String currentSessionContextClass;
	
	    @Bean
	    public Properties hibernateProps() {
	        Properties properties = new Properties();
	        properties.setProperty("hibernate.dialect", dialect);
	        properties.setProperty("hibernate.show_sql", showSql);
	        properties.setProperty("hibernate.format_sql", formatSql);
	        properties.setProperty("hibernate.hbm2ddl.auto", hbm2ddlAuto);
	        properties.setProperty("hibernate.current_session_context_class", currentSessionContextClass);
	        return properties;
	    }
	}
	spring.application.name=listener
	server.port=8765
	
	
	#DB
	datasource.url=jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
	datasource.driver-class-name=com.mysql.jdbc.Driver
	datasource.username=root
	datasource.password=root
	
	
	#ORM
	hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
	hibernate.show_sql=true
	hibernate.format_sql=true
	hibernate.hbm2ddl.auto=update
	hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext

1、HIbernate JPA 事务支持

	import org.springframework.beans.factory.annotation.Autowired;
	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.JpaVendorAdapter;
	import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
	import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
	
	import javax.persistence.EntityManagerFactory;
	import javax.sql.DataSource;
	import java.util.Properties;
	
	@EnableJpaRepositories(entityManagerFactoryRef = "jpaEntityManagerFactory", transactionManagerRef = "jpaTransactionManager", basePackages = {HibernateProperties.BASE_PACKAGES})
	@Configuration
	public class HibernateJpaTransactionConfig {
	
	    @Autowired
	    private Properties hibernateProps;
	
	    @Autowired
	    private DataSource dataSource;
	
	    @Bean(name = "jpaEntityManagerFactory")
	    public LocalContainerEntityManagerFactoryBean jpaEntityManagerFactory() {
	        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
	        em.setDataSource(dataSource);
	        em.setJpaProperties(hibernateProps);
	        em.setPackagesToScan(new String[]{HibernateProperties.BASE_PACKAGES});
	        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
	        em.setJpaVendorAdapter(vendorAdapter);
	        em.setPersistenceUnitName("jpa");
	        return em;
	    }
	
	    @Bean(name = "jpaTransactionManager")
	    public JpaTransactionManager jpaTransactionManager(EntityManagerFactory jpaEntityManagerFactory) {
	        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
	        jpaTransactionManager.setEntityManagerFactory(jpaEntityManagerFactory);
	        return jpaTransactionManager;
	    }
	}

2、Hibernate 自有事务支持

	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.context.annotation.Bean;
	import org.springframework.context.annotation.Configuration;
	import org.springframework.context.annotation.Primary;
	import org.springframework.orm.hibernate5.HibernateTransactionManager;
	import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
	
	import javax.sql.DataSource;
	import java.util.Properties;
	
	@Configuration
	public class HibernateTransactionConfig {
	
	    @Autowired
	    private DataSource dataSource;
	
	    @Autowired
	    private Properties hibernateProps;
	
	    @Bean
	    public LocalSessionFactoryBean sessionFactory() {
	        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
	        sessionFactoryBean.setDataSource(dataSource);
	        sessionFactoryBean.setHibernateProperties(hibernateProps);
	        sessionFactoryBean.setPackagesToScan(new String[]{HibernateProperties.BASE_PACKAGES});
	        return sessionFactoryBean;
	    }
	
		@Primary
	    @Bean
	    public HibernateTransactionManager transactionManager() {
	        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
	        transactionManager.setSessionFactory(sessionFactory().getObject());
	        return transactionManager;
	    }
	}

关闭 JPA 自动配置

	@SpringBootApplication(exclude = {HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class})

特别注意

事务管理器配置类事务支持
HibernateTransactionManagerLocalSessionFactoryBean + HibernateTransactionManager + DataSourceHibernate
JpaTransactionManagerLocalContainerEntityManagerFactoryBean + JpaTransactionManager + DataSourceJPA
  • 同时使用两个事务管理器的时候,@Transactional 中必须指明所使用的事务管理器
  • 低版本的 Springboot 中 HibernateTransactionManager 可以同时支持两种实现
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值