Hybernate Tutorial
The differences in how Hibernate is used in Spring, Spring Boot, and Spring Batch
There are some differences in how Hibernate is used in Spring, Spring Boot, and Spring Batch. Here’s an overview of the key differences:
1. Spring Framework:
In the traditional Spring Framework, Hibernate is integrated using a combination of XML configuration and Java-based configuration. You typically need to:
- Configure DataSource: Define the database connection properties.
- Set up SessionFactory: Define a
LocalSessionFactoryBean
to configure Hibernate’sSessionFactory
. - Transaction Management: Use Spring’s
@Transactional
annotation or configure transactions in XML. - DAO Layer: Implement Data Access Object (DAO) classes using Hibernate’s
Session
API.
Example XML Configuration:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- database properties -->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.example.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
2. Spring Boot:
Spring Boot simplifies the setup by using auto-configuration. It automatically configures Hibernate if it detects spring-boot-starter-data-jpa
in the classpath. Key differences include:
- Auto-configuration: Spring Boot auto-configures the
DataSource
,EntityManagerFactory
, andTransactionManager
. - Application Properties: Database properties and Hibernate configurations are typically specified in
application.properties
orapplication.yml
.
Example application.properties
:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
Example Java Configuration:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
3. Spring Batch:
When using Hibernate with Spring Batch, the integration is focused on using Hibernate for reading and writing data in batch jobs. Key points include:
- Batch Configuration: Define
ItemReader
,ItemProcessor
, andItemWriter
beans that use Hibernate. - Job Configuration: Configure Spring Batch jobs and steps, potentially using Hibernate for database interactions.
Example Batch Configuration:
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.core.transaction.JobRepositoryFactoryBean;
import org.springframework.batch.item.database.HibernateItemWriter;
import org.springframework.batch.item.database.builder.HibernateItemWriterBuilder;
import org.springframework.batch.item.database.builder.HibernatePagingItemReaderBuilder;
import org.springframework.batch.item.support.builder.CompositeItemProcessorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
// Configure sessionFactory properties
return sessionFactory;
}
@Bean
public HibernatePagingItemReader<MyEntity> reader() {
return new HibernatePagingItemReaderBuilder<MyEntity>()
.sessionFactory(sessionFactory().getObject())
.queryString("FROM MyEntity")
.pageSize(10)
.build();
}
@Bean
public HibernateItemWriter<MyEntity> writer() {
return new HibernateItemWriterBuilder<MyEntity>()
.sessionFactory(sessionFactory().getObject())
.build();
}
@Bean
public Job importUserJob(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
return jobBuilderFactory.get("importUserJob")
.start(step1(stepBuilderFactory))
.build();
}
@Bean
public Step step1(StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("step1")
.<MyEntity, MyEntity>chunk(10)
.reader(reader())
.processor(new MyProcessor())
.writer(writer())
.build();
}
}
Summary:
- Spring Framework: More manual configuration (XML/Java), flexibility in setup.
- Spring Boot: Simplified configuration with auto-configuration and
application.properties
. - Spring Batch: Focuses on batch processing, using Hibernate as
ItemReader
andItemWriter
.
Each approach leverages Hibernate in a way that suits the specific needs and conventions of the framework being used.
Annotations
The use of annotations with Hibernate in Spring, Spring Boot, and Spring Batch is largely consistent, as the annotations are primarily part of the JPA (Java Persistence API) specification that Hibernate implements. However, there are some nuances based on the framework. Here’s how annotations are used in each context:
1. Spring Framework:
In a traditional Spring application, you use JPA annotations on your entity classes and standard Spring annotations for configuration.
Entity Class Example:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// getters and setters
}
Configuration Class Example:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
public class HibernateConfig {
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
// configure sessionFactory properties
return sessionFactory;
}
}
2. Spring Boot:
Spring Boot further simplifies the use of annotations by leveraging auto-configuration, but the entity annotations remain the same. Spring Boot applications often use fewer explicit configuration annotations due to the auto-configuration capabilities.
Entity Class Example:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// getters and setters
}
Application Class Example:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Repository Interface Example:
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductRepository extends JpaRepository<Product, Long> {
}
3. Spring Batch:
In Spring Batch, annotations are used on the entity classes just like in other Spring contexts. Additionally, Spring Batch uses annotations to define batch components and steps.
Entity Class Example:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String product;
// getters and setters
}
Batch Configuration Example:
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.item.database.HibernateItemWriter;
import org.springframework.batch.item.database.builder.HibernateItemWriterBuilder;
import org.springframework.batch.item.database.builder.HibernatePagingItemReaderBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
// configure sessionFactory properties
return sessionFactory;
}
@Bean
public HibernatePagingItemReader<Order> reader() {
return new HibernatePagingItemReaderBuilder<Order>()
.sessionFactory(sessionFactory().getObject())
.queryString("FROM Order")
.pageSize(10)
.build();
}
@Bean
public HibernateItemWriter<Order> writer() {
return new HibernateItemWriterBuilder<Order>()
.sessionFactory(sessionFactory().getObject())
.build();
}
@Bean
public Job importOrderJob(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
return jobBuilderFactory.get("importOrderJob")
.incrementer(new RunIdIncrementer())
.flow(step1(stepBuilderFactory))
.end()
.build();
}
@Bean
public Step step1(StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("step1")
.<Order, Order>chunk(10)
.reader(reader())
.processor(new OrderProcessor())
.writer(writer())
.build();
}
}
Summary of Annotations:
- JPA Annotations: Common across all contexts for defining entity mappings (
@Entity
,@Id
,@GeneratedValue
, etc.). - Spring Core Annotations: Used for configuration and transaction management (
@Configuration
,@EnableTransactionManagement
,@Transactional
). - Spring Boot Annotations: Simplify configuration and setup (
@SpringBootApplication
). - Spring Batch Annotations: Specific to batch processing (
@EnableBatchProcessing
).
By using these annotations, developers can leverage the power of Hibernate with the appropriate Spring framework to handle data persistence efficiently.