Introduction and use of Spring Data JPA

Introduction to Spring Data JPA

Spring Data JPA overview

Spring Data JPA is a set of JPA application framework encapsulated by spring based on ORM framework and JPA specification, which enables developers to access and operate the database with minimal code. It provides common functions including addition, deletion, modification and query, and is easy to expand! Learning and using Spring Data JPA can greatly improve development efficiency!

Spring Data JPA frees us from the operation of DAO layer. Basically, all cruds can rely on it. In the actual work project, it is recommended to use Spring Data JPA + ORM (such as hibernate) to complete the operation, which provides great convenience when switching different ORM frameworks, and also makes the operation of database layer simpler and easier

Features of Spring Data JPA

Spring data JPA greatly simplifies the database access layer code. How to simplify it? Using spring data JPA, our dao layer only needs to write interfaces, and automatically has methods such as addition, deletion, modification and query, paging query and so on.

Official website: https://docs.spring.io/spring-data/jpa/docs/2.6.0/reference/html/#preface

Relationship between Spring Data JPA and JPA and hibernate

JPA is a set of specifications, which is internally composed of interfaces and abstract classes. Hibernate is a mature ORM framework, and Hibernate implements JPA specification, so it can also be called an implementation of JPA. We use JPA API programming, which means looking at problems from a higher perspective (interface oriented programming)

Spring Data JPA is a set of more advanced encapsulation of JPA operations provided by spring. It is a solution specially used for data persistence under the JPA specification.

Spring Data JPA usage

Build the development environment of Spring Data JPA

Coordinates of Spring Data JPA

To use Spring Data JPA, you need to integrate spring and Spring Data JPA, and you need to provide hibernate, the service provider of JPA, so you need to import spring related coordinates, hibernate coordinates, database driven coordinates, etc

<roperties>
    <spring.version>4.2.4.RELEASE</spring.version>
    <hibernate.version>5.0.7.Final</hibernate.version>
    <slf4j.version>1.6.6</slf4j.version>
    <log4j.version>1.2.12</log4j.version>
    <c3p0.version>0.9.1.2</c3p0.version>
    <mysql.version>5.1.6</mysql.version>
</properties>

<dependencies>
    <!-- junit unit testing  -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>
    
    <!-- spring beg -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.8</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring.version}</version>
    </dependency>

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

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

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

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    
    <!-- spring end -->

    <!-- hibernate beg -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.2.1.Final</version>
    </dependency>
    <!-- hibernate end -->

    <!-- c3p0 beg -->
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>${c3p0.version}</version>
    </dependency>
    <!-- c3p0 end -->

    <!-- log end -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <!-- log end -->

    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.9.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    
    <!-- el beg use spring data jpa Must be introduced -->
    <dependency>  
        <groupId>javax.el</groupId>  
        <artifactId>javax.el-api</artifactId>  
        <version>2.2.4</version>  
    </dependency>  
      
    <dependency>  
        <groupId>org.glassfish.web</groupId>  
        <artifactId>javax.el</artifactId>  
        <version>2.2.4</version>  
    </dependency> 
    <!-- el end -->
</dependencies>

Integrating Spring Data JPA with Spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/data/jpa
		http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

  <!--spring and spring data jpa Configuration of-->

  <!-- 1.establish entityManagerFactory Object to spring Container management -->
  <bean id="entityManagerFactoty" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!--Configured scanned package (package of entity class) -->
    <property name="packagesToScan" value="com.erlang.jpa.pojo" />
    <!-- jpa Implementation manufacturer -->
    <property name="persistenceProvider">
      <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
    </property>

    <!--jpa Vendor adapter for -->
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <!--Configure whether to automatically create database tables -->
        <property name="generateDdl" value="false" />
        <!--Specify database type -->
        <property name="database" value="MYSQL" />
        <!--Database Dialect: supported unique syntax -->
        <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
        <!--Show sql -->
        <property name="showSql" value="true" />
      </bean>
    </property>

    <!--jpa Dialects: advanced features -->
    <property name="jpaDialect" >
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>

  </bean>

  <!--2.Create database connection pool -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="user" value="root"></property>
    <property name="password" value="1234"></property>
    <property name="jdbcUrl" value="jdbc:mysql:///spring" ></property>
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  </bean>

  <!--3.integration spring dataJpa-->
  <jpa:repositories base-package="com.erlang.jpa.dao" transaction-manager-ref="transactionManager"
                    entity-manager-factory-ref="entityManagerFactoty" ></jpa:repositories>

  <!--4.Configure transaction manager -->
  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactoty"></property>
  </bean>

  <!-- 4.txAdvice-->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="save*" propagation="REQUIRED"/>
      <tx:method name="insert*" propagation="REQUIRED"/>
      <tx:method name="update*" propagation="REQUIRED"/>
      <tx:method name="delete*" propagation="REQUIRED"/>
      <tx:method name="get*" read-only="true"/>
      <tx:method name="find*" read-only="true"/>
      <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
  </tx:advice>

  <!-- 5.aop-->
  <aop:config>
    <aop:pointcut id="pointcut" expression="execution(* com.erlang.jpa.service.*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
  </aop:config>


  <!--5.Declarative transaction -->

  <!-- 6. Configure package scanning-->
  <context:component-scan base-package="com.erlang.jpa" ></context:component-scan>
</beans>

Configuring mapping relationships using JPA annotations

@Entity
	Role: Specifies that the current class is an entity class.
@Table
	Role: Specifies the correspondence between entity classes and tables.
@Id
	Function: Specifies that the current field is the primary key.
@GeneratedValue
	Function: Specifies the generation method of the primary key..
@Column
	Role: specify the correspondence between entity class attributes and database tables

Tags: Framework

Posted on Tue, 30 Nov 2021 23:51:27 -0500 by arag0rn