Home  >  Article  >  Java  >  How does Hibernate integrate with the Spring framework?

How does Hibernate integrate with the Spring framework?

王林
王林Original
2024-04-17 12:03:011065browse

To integrate Hibernate into the Spring framework, follow these steps: add Maven dependencies; configure the data source; create an EntityManager bean; configure the transaction manager; enable Persistent Exception Translation.

Hibernate 如何与 Spring 框架集成?

#How to integrate Hibernate into the Spring framework?

Introduction

Hibernate is a persistence framework for object-relational mapping (ORM), while Spring is a popular Java application framework . Integrating Hibernate into Spring simplifies database access and provides enterprise-grade functionality.

Integration steps

1. Add dependencies

In the pom.xml file of the Maven project, add the following dependencies Item:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

2. Configure data source

In the Spring configuration file (such as application.properties), configure the database connection details:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=user
spring.datasource.password=password

3. Create EntityManager Bean

Create an EntityManagerFactoryBean Bean to manage interaction with the database:

@Bean
public EntityManagerFactory entityManagerFactory() {
  HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  vendorAdapter.setGenerateDdl(true);

  LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
  factory.setDataSource(dataSource());
  factory.setJpaVendorAdapter(vendorAdapter);
  factory.setPackagesToScan("com.example.myproject.entity");
  return factory.getObject();
}

4. Configure transaction manager

Create a PlatformTransactionManager Bean to manage transactions:

@Bean
public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory emf) {
  JpaTransactionManager transactionManager = new JpaTransactionManager();
  transactionManager.setEntityManagerFactory(emf);
  return transactionManager;
}

5. Enable Persistent Exception Translation

Enable Persistent Exception by adding the following properties in the Spring configuration file Translation:

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

Practical case

The following Java code shows how to use Hibernate to save an entity:

@Entity
public class Person {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String name;
}

@Service
public class PersonService {
  @Autowired
  private EntityManagerFactory entityManagerFactory;

  public void save(Person person) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(person);
    entityManager.getTransaction().commit();
  }
}

The above is the detailed content of How does Hibernate integrate with the Spring framework?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn