在spring中使用Hibernate5
1. Overview
In this article, we’ll discuss how to bootstrap Hibernate 5 with Spring, using both Java and XML configuration.
2. Spring Integration
Bootstrapping a SessionFactory with the native Hibernate API is a bit complicated and would take us quite a few lines of code (have a look at the official documentation in case you really need to do that).
Fortunately, Spring supports bootstrapping the SessionFactory – so that we only need a few lines of Java code or XML configuration.
Also, before we jump in, if you’re working with older versions of Hibernate, you can have a look at the articles about Hibernate 3 as well as Hibernate 4 with Spring.
3. Maven Dependencies
Let’s get started by first adding the necessary dependencies to our pom.xml:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.2.Final</version>
</dependency>
The [spring-orm module](https://search.maven.org/classic/#search|gav|1|g%3A"org.springframework" AND a%3A"spring-orm") provides the Spring integration with Hibernate:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
For the sake of simplicity, we’ll use [H2](https://search.maven.org/classic/#search|gav|1|g%3A"com.h2database" AND a%3A"h2") as our database:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
Finally, we are going to use [Tomcat JDBC Connection Pooling](https://search.maven.org/classic/#search|gav|1|g%3A"org.apache.tomcat" AND a%3A"tomcat-dbcp"), which fits better for production purposes than the DriverManagerDataSource provided by Spring:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>9.0.1</version>
</dependency>
4. Configuration
As mentioned before, Spring supports us with bootstrapping the Hibernate SessionFactory.
All we have to do is to define some beans as well as a few parameters.
With Spring, we have two options for these configurations, a Java-based and an XML-based way.
4.1. Using Java Configuration
For using Hibernate 5 with Spring, little has changed since Hibernate 4: we have to use LocalSessionFactoryBeanfrom the package org.springframework.orm.hibernate5 instead of org.springframework.orm.hibernate4.
Like with Hibernate 4 before, we have to define beans for LocalSessionFactoryBean, DataSource, and PlatformTransactionManager, as well as some Hibernate-specific properties.
Let’s create our HibernateConfig class to configure Hibernate 5 with Spring:
@Configuration
@EnableTransactionManagement
public class HibernateConf {
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(
{"com.baeldung.hibernate.bootstrap.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
dataSource.setUsername("sa");
dataSource.setPassword("sa");
return dataSource;
}
@Bean
public PlatformTransactionManager hibernateTransactionManager() {
HibernateTransactionManager transactionManager
= new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
private final Properties hibernateProperties() {
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty(
"hibernate.hbm2ddl.auto", "create-drop");
hibernateProperties.setProperty(
"hibernate.dialect", "org.hibernate.dialect.H2Dialect");
return hibernateProperties;
}
}
4.2. Using XML Configuration
As a secondary option, we can also configure Hibernate 5 with an XML-based configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="...">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource"
ref="dataSource"/>
<property name="packagesToScan"
value="com.baeldung.hibernate.bootstrap.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">
create-drop
</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.H2Dialect
</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:db;DB_CLOSE_DELAY=-1"/>
<property name="username" value="sa"/>
<property name="password" value="sa"/>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
As we can easily see, we’re defining exactly the same beans and parameters as in the Java-based configuration earlier.
To bootstrap the XML into the Spring context, we can use a simple Java configuration file if the application is configured with Java configuration:
@Configuration
@EnableTransactionManagement
@ImportResource({"classpath:hibernate5Configuration.xml"})
public class HibernateXMLConf {
//
}
Alternatively, we can simply provide the XML file to the Spring Context, if the overall configuration is purely XML.
5. Usage
At this point, Hibernate 5 is fully configured with Spring, and we can inject the raw Hibernate SessionFactory directly whenever we need to:
public abstract class BarHibernateDAO {
@Autowired
private SessionFactory sessionFactory;
// ...
}
6. Supported Databases
Unfortunately, the Hibernate project doesn’t exactly provide an official list of supported databases.
That being said, it’s easy to see if a particular database type might be supported, we can have a look at the list of supported dialects.
7. Conclusion
In this quick tutorial, we configured Spring with Hibernate 5 – with both Java and XML configuration.
As always, the full source code of the examples is available over on GitHub.
最新文章
- oracle 存储过程的用法
- iOS 崩溃日志 Backtrace的符号化
- Nodejs&#183;内存控制
- 【leetcode❤python】 190. Reverse Bits
- Web:AJAX的详解
- 为什么要使用Hibernate
- Web前端的学习介绍(截止今天还有Bootstrap没有学,要腾点时间解决掉)
- 使用WCF服务的客户端出现maxReceivedMessageSize异常
- PLSQL插入数据中文乱码的问题
- onitemcommand 的作用以及onitemdatabound的具体作用
- Myeclipse自动生成javabean的get和set方法
- 教学小例子:简易的webSevrer
- 读redux有感: redux原来是这样操作的。
- netty常用使用方式
- servlet 监听器分类
- springboot2.0 最大上传文件大小遇到的错误Failed to bind properties under &#39;spring.servlet.multipart.max-file-size&#39;
- Linux c读取任意大小文件的所有数据
- 1.Linux进程--进程标识号
- AlphaRacks 2018年黑五 VPS $3.99/年
- Qt 之 pro 配置详解
热门文章
- vue项目进入mui.js报错 typeError: &#39;caller&#39;, &#39;callee&#39;, and &#39;arguments&#39; properties may not be accessed on strict mode .....
- PHP生成json
- MySQL视图及索引
- linux终端下常用快捷键
- python实现生产者消费者模型
- IDEA+SpringBoot项目启动参数设置
- Graylog-Sidecar
- Android: Error inflating class android.support.v4.view.ViewPager 问题的解决方法
- VMware下安装Ubuntu虚拟机
- mysql 之编码配置、引擎介绍、字段操作、数据类型及约束条件