In this article, we will learn how to integrate Spring Data JPA into our Spring application. We will be extending JPARepository and be creating an Employee management application and store the details using Oracle database.
Creating table
Create EMPLOYEE Table, simply Copy and Paste the following SQL query in the query editor to get the table created.
CREATE TABLE "EMPLOYEE" ( "ID" NUMBER(10) NOT NULL ENABLE, "AGE" NUMBER(10), "DEPT" VARCHAR2(255 CHAR), "NAME" VARCHAR2(255 CHAR), PRIMARY KEY ("ID") );
Folder Structure:
- Create a simple Maven Project “SpringDataJPA” by selecting maven-archetype-quickstart and create a package for our source files “com.javainterviewpoint” under src/main/java
- Now add the following dependency in the POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javainterviewpoint</groupId> <artifactId>SpringJPA</artifactId> <packaging>jar</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringJPA Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <hibernate.version>4.2.0.Final</hibernate.version> <mysql.connector.version>5.1.21</mysql.connector.version> <spring.version>4.3.5 RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>${cglib.version}</version> </dependency> <!-- DB related dependencies --> <dependency> <groupId>org.hibernate.common</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>4.0.4.Final</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.1.GA</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency> <!-- SPRING --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.5.RELEASE</version> </dependency> <!-- CGLIB is required to process @Configuration classes --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> <!-- Servlet API and JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.5.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test-mvc</artifactId> <version>1.0.0.M1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.11.3.RELEASE</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>11.2.0</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.9.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.3.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.5.RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.1.9.Final</version> </dependency> </dependencies> <build> <finalName>SpringJPA</finalName> </build> </project>
- Create the Java classes EmployeeService.java, EmployeeServiceImpl.java, EmployeeRepository.java, Employee.java, and App.java under com.javainterviewpoint folder.
Spring Data JPA CRUD Example
EmployeeRepository.java
package com.javainterviewpoint; import org.springframework.data.jpa.repository.JpaRepository; public interface EmployeeRepository extends JpaRepository<Employee,Integer> { }
We have extended JpaRepository in our EmployeeRepository class, that’s all we need to do. We will be able to perform CRUD Operations using the built-in methods of JpaRepository.
EmployeeService.java
package com.javainterviewpoint; import java.util.List; public interface EmployeeService { public abstract Employee getEmployeeById(int id); public abstract void saveEmployee(Employee employee); public abstract void updateEmployee(Employee employee); public abstract void deleteEmployee(int id); public abstract List<Employee> getAllEmployees(); }
EmployeeService is a simple interface to perform CRUD Operation, implementation will be provided by EmployeeServiceImpl class.
EmployeeServiceImpl.java
package com.javainterviewpoint; 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 EmployeeServiceImpl implements EmployeeService { @Autowired private EmployeeRepository employeeRepository; @Transactional public Employee getEmployeeById(int id) { return employeeRepository.findOne(id); } @Transactional public void saveEmployee(Employee employee) { employeeRepository.save(employee); } @Transactional public void updateEmployee(Employee employee) { employeeRepository.save(employee); } @Transactional public void deleteEmployee(int id) { employeeRepository.delete(id); } @Transactional public List<Employee> getAllEmployees() { return employeeRepository.findAll(); } }
We have implemented the EmployeeService interface and auto-wired EmployeeRepository class. We will be using findOne(), save(), delete(), findAll() method of JpaRepository class to perform CRUD.
SpringConfig.xml
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx/ http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd"> <context:component-scan base-package="com.javainterviewpoint"></context:component-scan> <jpa:repositories base-package="com.javainterviewpoint" entity-manager-factory-ref="entityManagerFactoryBean"></jpa:repositories> <bean id="employeeServiceImpl" class="com.javainterviewpoint.EmployeeServiceImpl" /> <!--EntityManagerFactory --> <bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- This makes /META-INF/persistence.xml is no longer necessary --> <property name="packagesToScan" value="com.javainterviewpoint" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> </property> <property name="jpaProperties"> <props> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> </props> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@rsh2:40051:mydb" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactoryBean" /> </bean> <tx:annotation-driven /> </beans>
Below are the configurations which has been made in the above config file.
- Configure the data source bean
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@rsh2:40051:mydb" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean>
- Configure the entity manager factory bean
<bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- This makes /META-INF/persistence.xml is no longer necessary --> <property name="packagesToScan" value="com.javainterviewpoint" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> </property> <property name="jpaProperties"> <props> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> </props> </property> </bean>
- Configure the transaction manager bean
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactoryBean" /> </bean>
- In the <jpa:repositories> tag we need to pass our “entityManagerFactoryBean” reference
<jpa:repositories base-package="com.javainterviewpoint" entity-manager-factory-ref="entityManagerFactoryBean"></jpa:repositories>
App.java
package com.javainterviewpoint; import java.util.List; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { //Reading the Configuration file ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("springConfig.xml"); //Get the EmployeeServiceImpl bean EmployeeServiceImpl employeeServiceImpl = (EmployeeServiceImpl)context.getBean("employeeServiceImpl"); //Create Employee object Employee employee = new Employee(); employee.setAge(55); employee.setDept("Blogging"); employee.setName("JIP"); //Save the new Employee employeeServiceImpl.saveEmployee(employee); //Read the Employee Employee emp = employeeServiceImpl.getEmployeeById(1); //Retrieve all the Employees List<Employee> employeeList = employeeServiceImpl.getAllEmployees(); System.out.println("*** List of all Employees *** "); for(Employee emp1 : employeeList) { System.out.println("Employee Id :"+emp1.getId()); System.out.println("Employee Name :"+emp1.getName()); System.out.println("Employee Age :"+emp1.getAge()); System.out.println("Department :"+emp1.getDept()); System.out.println(); } System.out.println("*************************************"); //Delete Employee employeeServiceImpl.deleteEmployee(1); context.close(); } }
- ClassPathXmlApplicationContext reads our Configuration File(SpringConfig.xml)
- BeanFactory class read all the bean definition mentioned in the config file.
- Get the EmployeeServiceImpl Class instance by calling the getBean() method over the bean factory.
- We will be calling the saveEmployee(), getEmployeeById(), getAllEmployees() and deleteEmployee() of the EmployeeServiceImpl Class.
Employee.java
package com.javainterviewpoint; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="EMPLOYEE") public class Employee implements Serializable { private static final long serialVersionUID = -889976693182180703L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="ID") private int id; @Column(name="NAME") private String name; @Column(name="AGE") private int age; @Column(name="DEPT") private String dept; public Employee() { super(); } public Employee(int id, String name, int age, String dept) { super(); this.id = id; this.name = name; this.age = age; this.dept = dept; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", dept=" + dept + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((dept == null) ? 0 : dept.hashCode()); result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; if (age != other.age) return false; if (dept == null) { if (other.dept != null) return false; } else if (!dept.equals(other.dept)) return false; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
Our Employee class is a simple POJO class consisting getters and setters of its properties.
Output
*** List of all Employees *** Employee Id :1 Employee Name :JIP1 Employee Age :99 Department :IT Employee Id :4 Employee Name :aaaa Employee Age :4 Department :test Employee Id :3 Employee Name :JIP3333 Employee Age :33333 Department :IT Employee Id :141 Employee Name :JIP Employee Age :55 Department :Blogging *************************************
arun singh says
Thanks, a big help