Spring ORM示例 – JPA,Hibernate,事务

欢迎来到Spring ORM示例教程。今天我们将学习使用Hibernate JPA事务管理的Spring ORM示例。我将向您展示一个非常简单的Spring独立应用程序示例,具有以下功能。

  • 依赖注入(@Autowired注解)
  • JPA EntityManager(由Hibernate提供)
  • 注释的事务方法(@Transactional注解)

Spring ORM示例

我已经在 Spring ORM 示例中使用了内存数据库,因此不需要任何数据库设置(但您可以在 spring.xml 数据源部分将其更改为其他数据库)。这是一个 Spring ORM 独立应用程序,以尽量减少所有依赖项(但如果您熟悉 Spring,可以通过配置轻松将其更改为 Web 项目)。注意:对于基于 Spring AOP 的事务(无需 @Transactional 注解)的方法解析方法,请查看此教程:Spring ORM AOP 事务管理。下图显示了我们最终的 Spring ORM 示例项目。 让我们逐个查看每个 Spring ORM 示例项目组件。

Spring ORM Maven 依赖项

以下是我们最终的 pom.xml 文件,其中包含 Spring ORM 依赖项。我们在 Spring ORM 示例中使用了 Spring 4 和 Hibernate 4。

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>hu.daniel.hari.learn.spring</groupId>
	<artifactId>Tutorial-SpringORMwithTX</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<properties>
		<!-- Generic properties -->
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.7</java.version>

		<!-- SPRING & HIBERNATE / JPA -->
		<spring.version>4.0.0.RELEASE</spring.version>
		<hibernate.version>4.1.9.Final</hibernate.version>

	</properties>

	<dependencies>
		<!-- LOG -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>

		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<!-- JPA Vendor -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

		<!-- IN MEMORY Database and JDBC Driver -->
		<dependency>
			<groupId>hsqldb</groupId>
			<artifactId>hsqldb</artifactId>
			<version>1.8.0.7</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>
  • 系统: 我们需要 spring-contextspring-orm 作为 Spring 的依赖。
  • 我们使用 hibernate-entitymanager 作为 Hibernate 的 JPA 实现。hibernate-entitymanager 依赖于 hibernate-core,这就是为什么我们不必在 pom.xml 中显式放置 hibernate-core。它通过 Maven 传递依赖项被引入到我们的项目中。
  • 我们还需要 JDBC 驱动作为数据库访问的依赖项。我们使用包含 JDBC 驱动和可工作的内存数据库的 HSQLDB。

Spring ORM 模型类

我们可以在模型 bean 中使用标准的 JPA 注解进行映射,因为 Hibernate 提供了 JPA 实现。

package hu.daniel.hari.learn.spring.orm.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Product {

	@Id
	private Integer id;
	private String name;

	public Product() {
	}

	public Product(Integer id, String name) {
		this.id = id;
		this.name = name;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Product [id=" + id + ", name=" + name + "]";
	}

}

我们使用 @Entity@Id JPA 注解来将我们的 POJO 标记为实体并定义其主键。

Spring ORM DAO 类

我们创建一个非常简单的 DAO 类,提供了持久化和查找所有方法。

package hu.daniel.hari.learn.spring.orm.dao;

import hu.daniel.hari.learn.spring.orm.model.Product;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;

@Component
public class ProductDao {

	@PersistenceContext
	private EntityManager em;

	public void persist(Product product) {
		em.persist(product);
	}

	public List<Product> findAll() {
		return em.createQuery("SELECT p FROM Product p").getResultList();
	}

}
  • @Component 是 Spring 注解,告诉 Spring 容器我们可以通过 Spring IoC(依赖注入)使用这个类。
  • 我们使用 JPA 的 `@PersistenceContext` 注解来指示将 EntityManager 注入依赖项。Spring 根据 spring.xml 配置注入一个合适的 EntityManager 实例。

Spring ORM 服务类

我们简单的服务类有 2 个写入方法和 1 个读取方法 – add、addAll 和 listAll。

package hu.daniel.hari.learn.spring.orm.service;

import hu.daniel.hari.learn.spring.orm.dao.ProductDao;
import hu.daniel.hari.learn.spring.orm.model.Product;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class ProductService {

	@Autowired
	private ProductDao productDao;

	@Transactional
	public void add(Product product) {
		productDao.persist(product);
	}
	
	@Transactional
	public void addAll(Collection<Product> products) {
		for (Product product : products) {
			productDao.persist(product);
		}
	}

	@Transactional(readOnly = true)
	public List<Product> listAll() {
		return productDao.findAll();

	}

}
  • 我们使用 Spring 的 `@Autowired` 注解在我们的服务类中注入 ProductDao。
  • 我们希望使用事务管理,因此方法被标注为 `@Transactional` Spring 注解。listAll 方法仅读取数据库,因此我们将 `@Transactional` 注解设置为只读以进行优化。

Spring ORM 示例 Bean 配置 XML

我们的 Spring ORM 示例项目 Java 类已准备就绪,现在让我们来看一下我们的 Spring Bean 配置文件。`spring.xml`

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans" 
	xmlns:p="https://www.springframework.org/schema/p"
	xmlns:context="https://www.springframework.org/schema/context" 
	xmlns:tx="https://www.springframework.org/schema/tx" 
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
		https://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		https://www.springframework.org/schema/context
		https://www.springframework.org/schema/context/spring-context-3.0.xsd
		https://www.springframework.org/schema/tx
		https://www.springframework.org/schema/tx/spring-tx.xsd
		">
	
	<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans -->
	<context:component-scan base-package="hu.daniel.hari.learn.spring" />
	<!-- Activates various annotations to be detected in bean classes e.g: @Autowired -->
	<context:annotation-config />

	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
		<property name="url" value="jdbc:hsqldb:mem://productDb" />
		<property name="username" value="sa" />
		<property name="password" value="" />
	</bean>
	
	<bean id="entityManagerFactory" 
			class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
			p:packagesToScan="hu.daniel.hari.learn.spring.orm.model"
            p:dataSource-ref="dataSource"
			>
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="generateDdl" value="true" />
				<property name="showSql" value="true" />
			</bean>
		</property>
	</bean>

	<!-- Transactions -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
	<!-- enable the configuration of transactional behavior based on annotations -->
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>
  1. 首先,我们告诉 Spring 我们要使用类路径扫描来扫描 Spring 组件(服务、DAO),而不是在 spring xml 中逐个定义它们。我们还启用了 Spring 注解检测。
  2. 添加数据源,目前使用的是 HSQLDB 内存数据库。
  3. 我们设置了一个 JPA EntityManagerFactory,应用程序将使用它来获取一个 EntityManager。Spring 支持 3 种不同的方式来做到这一点,我们使用了 LocalContainerEntityManagerFactoryBean 来获取完整的 JPA 功能。我们设置了 LocalContainerEntityManagerFactoryBean 的属性如下:
    1. packagesToScan 属性指向我们的模型类包。
    2. 数据源在 spring 配置文件中早先定义。
    3. jpaVendorAdapter 为 Hibernate 并设置了一些 Hibernate 属性。
  4. 我们创建了一个 Spring PlatformTransactionManager 实例作为 JpaTransactionManager。这个事务管理器适用于使用单个 JPA EntityManagerFactory 进行事务性数据访问的应用程序。
  5. 我们启用了基于注解的事务行为配置,并设置了我们创建的 transactionManager。

Spring ORM Hibernate JPA 示例测试程序

我们的 Spring ORM JPA Hibernate 示例项目已经准备好了,现在让我们为我们的应用程序编写一个测试程序。

public class SpringOrmMain {
	
	public static void main(String[] args) {
		
		//创建Spring应用上下文
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/spring.xml");
		
		//从上下文获取服务(服务的依赖项(ProductDAO)在ProductService中自动装配)
		ProductService productService = ctx.getBean(ProductService.class);
		
		//进行一些数据操作
		
		productService.add(new Product(1, "Bulb"));
		productService.add(new Product(2, "Dijone mustard"));
		
		System.out.println("listAll: " + productService.listAll());
		
		//测试事务回滚(重复的键)
		
		try {
			productService.addAll(Arrays.asList(new Product(3, "Book"), new Product(4, "Soap"), new Product(1, "Computer")));
		} catch (DataAccessException dataAccessException) {
		}
		
		//回滚后测试元素列表
		System.out.println("listAll: " + productService.listAll());
		
		ctx.close();
		
	}
}

您可以看到我们如何从主方法轻松启动Spring容器。我们得到了我们的第一个依赖注入的入口点,服务类实例。在Spring上下文初始化后,将ProductDao类引用注入ProductService类。在我们得到ProductService实例之后,我们可以测试它的方法,由于Spring的代理机制,所有方法调用都将是事务性的。我们还在这个示例中测试了回滚。如果您运行上述Spring ORM示例测试程序,您将得到以下日志。

Hibernate: insert into Product (name, id) values (?, ?)
Hibernate: insert into Product (name, id) values (?, ?)
Hibernate: select product0_.id as id0_, product0_.name as name0_ from Product product0_
listAll: [Product [id=1, name=Bulb], Product [id=2, name=Dijone mustard]]
Hibernate: insert into Product (name, id) values (?, ?)
Hibernate: insert into Product (name, id) values (?, ?)
Hibernate: insert into Product (name, id) values (?, ?)
Hibernate: select product0_.id as id0_, product0_.name as name0_ from Product product0_
listAll: [Product [id=1, name=Bulb], Product [id=2, name=Dijone mustard]]

请注意,第二个事务被回滚,这就是为什么产品列表没有更改的原因。如果您使用附带源文件的log4j.properties文件,您可以看到底层发生了什么。参考:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/orm.html 您可以从下面的链接下载最终的Spring ORM JPA Hibernate示例项目,并进行更多学习。

下载带事务的Spring ORM项目

Source:
https://www.digitalocean.com/community/tutorials/spring-orm-example-jpa-hibernate-transaction