Springboot 学习之 JPA + Hibernate 双实现
前言
有时,我们希望在项目中 同时支持
HIbernate JPA
和 Hibernate 自有
的 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})
特别注意
事务管理器 | 配置类 | 事务支持 |
---|---|---|
HibernateTransactionManager | LocalSessionFactoryBean + HibernateTransactionManager + DataSource | Hibernate |
JpaTransactionManager | LocalContainerEntityManagerFactoryBean + JpaTransactionManager + DataSource | JPA |