{"id":75813,"date":"2018-05-03T01:00:11","date_gmt":"2018-05-02T22:00:11","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=75813"},"modified":"2023-12-11T10:47:52","modified_gmt":"2023-12-11T08:47:52","slug":"spring-data-jpa-tutorial","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html","title":{"rendered":"Spring Data JPA Tutorial"},"content":{"rendered":"<p>Managing data between java classes or objects and the relational database is a very cumbersome and tricky task. The DAO layer usually contains a lot of boilerplate code that should be simplified in order to reduce the number of lines of code and make the code reusable.<\/p>\n<p>In this tutorial, we will discuss spring data&#8217;s implementation of JPA.<\/p>\n<h2>1. Introduction<\/h2>\n<h3>1.1 What is JPA?<\/h3>\n<p>JPA or Java Persistence API is the Java specification for accessing, managing and persisting data between Java classes or objects and relational database. The specification was introduced as part of EJB 3.0.<\/p>\n<p>JPA is not an implementation or product, it is just a specification. It contains set of interfaces which need to be implemented. It is a framework that provides an extra layer of abstraction on the JPA implementation. The repository layer will contain three layers as mentioned below.<\/p>\n<ul>\n<li>Spring Data JPA: &#8211; This provides spring data repository interfaces which are implemented to create JPA repositories.<\/li>\n<li>Spring Data Commons: &#8211; It provides the infrastructure that is shared between data store specific spring data projects.<\/li>\n<li>The JPA provider which implements the JPA persistence API.<\/li>\n<\/ul>\n<p>Spring data JPA allows us not to write any boilerplate code by adding an additional repository layer.<\/p>\n<h3>1.2 History of JPA<\/h3>\n<p><strong>JPA2.0: &#8211;\u00a0<\/strong>The development of JPA2.0 started in 2007 as JSR 317. The version was marked as 2.0 as it could not gain the common consensus for 1.0. The focus was to address the features available as part of famous vendor ORMs.<\/p>\n<p><strong>JPA 2.1:-\u00a0<\/strong>JPA2.1 started in July 2011 as JSR 338. Some of the main features were Entity graph, stored procedures, converters, etc.<\/p>\n<p><strong>JPA 2.2:-<\/strong>\u00a0JPA2.2 is the latest addition to JPA series in 2017. It includes support for Java 8 date and time type, ability to stream a query result.<\/p>\n<p>JPA is still undergoing lot of changes and we can epect a newer version of JPA soon.<\/p>\n<h3>1.3 Spring Data Repositories<\/h3>\n<p>Spring Data Commons project provides repository abstraction which is extended by the datastore-specific subprojects.<\/p>\n<p>We have to be familiar with the Spring Data repository interfaces as it will help us with the implementation of the interfaces. Let&#8217;s have a look at the interfaces.<\/p>\n<p><strong>Spring Data Commons: &#8211;<\/strong>\u00a0Following interfaces are provided as part of this project:<\/p>\n<ul>\n<li><code><a href=\"http:\/\/docs.spring.io\/spring-data\/commons\/docs\/current\/api\/index.html?org\/springframework\/data\/repository\/Repository.html\" target=\"_blank\" rel=\"noopener\"><em>Repository&lt;T, ID extends Serializable&gt;<\/em><\/a><\/code>\u00a0 :\u00a0This interface is a marker interface.\n<ol>\n<li>It captures the type of the managed entity and the type of the entity\u2019s id.<\/li>\n<li>It helps the Spring container to discover the \u201cconcrete\u201d repository interfaces when classpath is scanned.<\/li>\n<\/ol>\n<\/li>\n<li><code><a href=\"http:\/\/docs.spring.io\/spring-data\/commons\/docs\/current\/api\/org\/springframework\/data\/repository\/CrudRepository.html\" target=\"_blank\" rel=\"noopener\"><em>CrudRepository&lt;T, ID extends Serializable&gt;<\/em><\/a><\/code>\u00a0:\u00a0It provides CRUD operations for the managed entity.<\/li>\n<li><code><a href=\"http:\/\/docs.spring.io\/spring-data\/commons\/docs\/current\/api\/org\/springframework\/data\/repository\/PagingAndSortingRepository.html\" target=\"_blank\" rel=\"noopener\"><em>PagingAndSortingRepository&lt;T, ID extends Serializable&gt;<\/em><\/a><\/code>\u00a0:\u00a0This interface declares the methods that are used to sort and paginate entities that are retrieved from the database.<\/li>\n<li><code><a href=\"http:\/\/docs.spring.io\/spring-data\/commons\/docs\/current\/api\/org\/springframework\/data\/querydsl\/QueryDslPredicateExecutor.html\" target=\"_blank\" rel=\"noopener\"><em>QueryDslPredicateExecutor&lt;T&gt;<\/em><\/a><\/code>\u00a0: It is not a \u201crepository interface\u201d. It declares the methods that are used to retrieve entities from the database by using\u00a0<code><a href=\"http:\/\/www.querydsl.com\/\" target=\"_blank\" rel=\"noopener\">QueryDsl<\/a><\/code>\u00a0<em>Predicate<\/em>\u00a0objects.<\/li>\n<\/ul>\n<p><strong>Spring Data JPA: &#8211;<\/strong>\u00a0 This project provides the following interfaces:<\/p>\n<ul>\n<li><code><a href=\"http:\/\/docs.spring.io\/spring-data\/jpa\/docs\/current\/api\/org\/springframework\/data\/jpa\/repository\/JpaRepository.html\" target=\"_blank\" rel=\"noopener\"><em>JpaRepository&lt;T, ID extends Serializable&gt;<\/em><\/a><\/code>\u00a0 :\u00a0This interface is a JPA specific repository interface that combines the methods declared by the common repository interfaces behind a single interface.<\/li>\n<li><code><a href=\"http:\/\/docs.spring.io\/spring-data\/jpa\/docs\/current\/api\/org\/springframework\/data\/jpa\/repository\/JpaSpecificationExecutor.html\" target=\"_blank\" rel=\"noopener\"><em>JpaSpecificationExecutor&lt;T&gt;<\/em><\/a><\/code>\u00a0: This is again not a \u201crepository interface\u201d. It declares the methods that are used to retrieve entities from the database by using\u00a0<code><a href=\"http:\/\/docs.spring.io\/spring-data\/jpa\/docs\/current\/api\/org\/springframework\/data\/jpa\/domain\/Specification.html\" target=\"_blank\" rel=\"noopener\"><em>Specification&lt;T&gt;<\/em><\/a><\/code>\u00a0objects that use the JPA criteria API.<\/li>\n<\/ul>\n<p>The repository hierarchy looks as follows:<\/p>\n<p><figure id=\"attachment_76282\" aria-describedby=\"caption-attachment-76282\" style=\"width: 809px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/springdatajrepositories_jcg.jpg\"><img decoding=\"async\" class=\"size-full wp-image-76282\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/springdatajrepositories_jcg.jpg\" alt=\"\" width=\"809\" height=\"451\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/springdatajrepositories_jcg.jpg 809w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/springdatajrepositories_jcg-300x167.jpg 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/springdatajrepositories_jcg-768x428.jpg 768w\" sizes=\"(max-width: 809px) 100vw, 809px\" \/><\/a><figcaption id=\"caption-attachment-76282\" class=\"wp-caption-text\">Spring Data Repository Hierarchy<\/figcaption><\/figure><\/p>\n<p>Let&#8217;s try to understand Spring Data JPA through a sample program.<\/p>\n<h3>1.4 Spring Data Custom Query<\/h3>\n<p>Let us consider a use case where we have to fetch data from a database based on a query. Writing a custom query is a very useful case. Spring Data JPA has different ways of writing custom queries. We will broadly classify the ways as mentioned below.<\/p>\n<p><strong>Automatic Custom Queries:<\/strong> The automatic custom query creation is also known as query creation from the method name.\u00a0Spring Data JPA has a built-in mechanism for query creation which can be used for parsing queries straight from the method name of a query method. The mechanism first removes common prefixes from the method name and parses the constraints of the query from the rest of the method name. In order to use this approach, we have to make sure\u00a0the method names of your repository interface are created by combining the property names of an entity object and the supported keywords.<\/p>\n<p>The advantage of using this approach is that it makes it very easy to implement. But the limitation is if the query contains more than one parameter than the method name will be not readable. Also the keywords that are not supported by JPA like lower will not be useful with this approach.<\/p>\n<p><strong>Manual Custom Queries:<\/strong> Manual Custom queries are also known as queries creation using <code>@Query<\/code> tag.\u00a0The <code>@Query<\/code> annotation will be used to create queries using the JPA query language and for binding these queries directly to the methods of your repository interface. When the query method is called, Spring Data JPA will execute the query specified by the <code>@Query<\/code> annotation.<\/p>\n<p>The advantage of this approach is you can use JPA query language for the creation of a query. Also, the query stays in repository layer. Limitation of this approach is <code>@Query<\/code> can be only used when JPA query language is supported.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>The program mentioned below uses both the approaches.<\/p>\n<h3>1.5 Spring Data JPA Exception Translation<\/h3>\n<p>The point to consider is\u00a0because the default Spring ORM templates are not used with SPring JPA, are we losing exception translation by using Spring Data JPA?nAre we not going to get our JPA exceptions translated to Spring\u2019s DataAccessException hierarchy?<\/p>\n<p>The answer is <strong>No<\/strong>. Exception translation is still enabled by the use of the annotation<code>@Repository<\/code> on the DAO. The annotation enables a Spring bean postprocessor to advice\u00a0all <code>@Repository<\/code> beans with all the <code>PersistenceExceptionTranslator<\/code> instances found in the Container and provides exception translation just as before.<\/p>\n<h2>2. Tools and Technologies<\/h2>\n<p>Let us look at the technologies and tool used for building the program.<\/p>\n<ul>\n<li>Eclipse\u00a0Oxygen.2 Release (4.7.2)<\/li>\n<li>Java \u2013 version 9.0.4<\/li>\n<li>Maven &#8211; 3.5.3<\/li>\n<li>Spring boot &#8211; 2.0.1-Release<\/li>\n<li>Postgresql &#8211; 10<\/li>\n<li>postman<\/li>\n<\/ul>\n<h2>3. Project Structure<\/h2>\n<p>Our project structure will look as shown in the image below.<\/p>\n<p><figure id=\"attachment_76215\" aria-describedby=\"caption-attachment-76215\" style=\"width: 419px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/ProjectStructure_JCG-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-76215\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/ProjectStructure_JCG-1.jpg\" alt=\"\" width=\"419\" height=\"435\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/ProjectStructure_JCG-1.jpg 419w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/ProjectStructure_JCG-1-289x300.jpg 289w\" sizes=\"(max-width: 419px) 100vw, 419px\" \/><\/a><figcaption id=\"caption-attachment-76215\" class=\"wp-caption-text\">Project Structure for Spring Data JPA Tutorial<\/figcaption><\/figure><\/p>\n<p>&nbsp;<\/p>\n<p>The above project structure is using Maven. This project can also be created using Gradle and the pom.xml will get replaced with build.gradle file. The structure of the project will defer slightly with the usage of Gradle for the build.<\/p>\n<h2>4. An objective of the Program<\/h2>\n<p>As part of the program, we will try to create a simple web service using spring boot. This web service will be used for data manipulation on PostgreSQL database.<\/p>\n<h3>4.1 pom.xml<\/h3>\n<p>The <code>pom.xml<\/code> file for the program will look as mentioned below.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>pom.xml for SpringData JPA with Spring Boot<\/em><\/span><\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n\txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n\n\t&lt;groupId&gt;com.springjpa&lt;\/groupId&gt;\n\t&lt;artifactId&gt;SpringJPA-PostgreSQL&lt;\/artifactId&gt;\n\t&lt;version&gt;0.0.1&lt;\/version&gt;\n\t&lt;packaging&gt;jar&lt;\/packaging&gt;\n\n\t&lt;name&gt;SpringJPA-PostgreSQL&lt;\/name&gt;\n\t&lt;description&gt;Demo project for Spring Boot JPA - PostgreSQL&lt;\/description&gt;\n\n\t&lt;parent&gt;\n\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n\t\t&lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\n\t\t&lt;version&gt;2.0.1.RELEASE&lt;\/version&gt;\n\t\t&lt;relativePath\/&gt; \n\t&lt;\/parent&gt;\n\n\t&lt;properties&gt;\n\t\t&lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\n\t\t&lt;project.reporting.outputEncoding&gt;UTF-8&lt;\/project.reporting.outputEncoding&gt;\n\t\t&lt;java.version&gt;1.9&lt;\/java.version&gt;\n\t&lt;\/properties&gt;\n\n\t&lt;dependencies&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;\/artifactId&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n\t\t&lt;\/dependency&gt;\n\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.postgresql&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;postgresql&lt;\/artifactId&gt;\n\t\t\t&lt;scope&gt;runtime&lt;\/scope&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt;\n\t\t\t&lt;scope&gt;test&lt;\/scope&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n  &lt;groupId&gt;javax.xml.bind&lt;\/groupId&gt;\n  &lt;artifactId&gt;jaxb-api&lt;\/artifactId&gt;\n  &lt;version&gt;2.3.0&lt;\/version&gt;\n&lt;\/dependency&gt;\n\t&lt;\/dependencies&gt;\n\n\t&lt;build&gt;\n\t\t&lt;plugins&gt;\n\t\t\t&lt;plugin&gt;\n\t\t\t\t&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n\t\t\t\t&lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;\n\t\t\t&lt;\/plugin&gt;\n\t\t&lt;\/plugins&gt;\n\t&lt;\/build&gt;\n\t\n\n\n&lt;\/project&gt;\n<\/pre>\n<p>The pom.xml file will contain the dependencies that will be needed as part of the program.<\/p>\n<h3>4.2 Application class<\/h3>\n<p>The Application.java class for us is\u00a0<code>SpringJpaPostgreSqlApplication.java<\/code> class. The class looks as shown below.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>SpringJpaPostgreSqlApplication.java class for Spring Boot<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.tutorial;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.CommandLineRunner;\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\n\nimport com.tutorial.repo.EmployeeRepository;\n\n@SpringBootApplication\npublic class SpringJpaPostgreSqlApplication implements CommandLineRunner{\n\n\t@Autowired\n\tEmployeeRepository repository;\n\t\n\tpublic static void main(String[] args){\n\t\tSpringApplication.run(SpringJpaPostgreSqlApplication.class, args);\n\t}\n\n\tpublic void run(String... args) throws Exception {\n\t\t\/\/ TODO Auto-generated method stub\n\t\t\n\t}\n\n}\n<\/pre>\n<p>The <code>CommandLineRunner<\/code> interface is used for to indicate that a bean should run when it is contained within a Spring Application.<\/p>\n<h3>4.3 Model Class<\/h3>\n<p>We are going to create an <code>Employee.java<\/code> class as the model class. The class will look as shown below.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Model class for the program<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.tutorial.model;\n\nimport java.io.Serializable;\n\nimport javax.persistence.Column;\nimport javax.persistence.Entity;\nimport javax.persistence.GeneratedValue;\nimport javax.persistence.GenerationType;\nimport javax.persistence.Id;\nimport javax.persistence.Table;\n\n@Entity\n@Table(name = \"employee\")\npublic class Employee implements Serializable {\n\n\tprivate static final long serialVersionUID = -3009157732242241606L;\n\t@Id\n\t@GeneratedValue(strategy = GenerationType.AUTO)\n\t@Column(name=\"id\")\n\tprivate long id;\n\n\t@Column(name = \"firstname\")\n\tprivate String firstName;\n\n\t@Column(name = \"lastname\")\n\tprivate String lastName;\n\t\n\t@Column(name = \"age\")\n\tprivate int age;\n\n\tprotected Employee() {\n\t}\n\n\tpublic Employee(String firstName, String lastName,int age) {\n\t\tthis.firstName = firstName;\n\t\tthis.lastName = lastName;\n\t\tthis.age = age;\n\t}\n\n\t@Override\n\tpublic String toString() {\n\t\treturn String.format(\"Employee[id=%d, firstName='%s', lastName='%s', age='%d']\", id, firstName, lastName,age);\n\t}\n\n\t\n\tpublic long getId() {\n\t\treturn id;\n\t}\n\n\tpublic void setId(long id) {\n\t\tthis.id = id;\n\t}\n\n\tpublic String getFirstName() {\n\t\treturn firstName;\n\t}\n\n\tpublic void setFirstName(String firstName) {\n\t\tthis.firstName = firstName;\n\t}\n\n\tpublic String getLastName() {\n\t\treturn lastName;\n\t}\n\n\tpublic void setLast_Name(String lastName) {\n\t\tthis.lastName = lastName;\n\t}\n\n\tpublic int getAge() {\n\t\treturn age;\n\t}\n\n\tpublic void setAge(int age) {\n\t\tthis.age = age;\n\t}\n}\n<\/pre>\n<p><code>@Entity<\/code>: Is used to define that the class is an Entity class.<br \/>\n<code>@Table<\/code>: This annotation is used for specifying the table name which is defined in the database.<br \/>\n<code>@Id<\/code>: Id annotation is used for specifying the Id attribute<br \/>\n<code>@GeneratedValue<\/code>: This is used when we want to set automatically generated value. <code>GenerationType<\/code> is the mechanism that is used for generating the value for the specific column.<br \/>\n<code>@Column<\/code>: This annotation is used for mapping the columns from the table with the attributes in the class.<br \/>\n[ulp id=&#8217;c4qGwqz2zZ7CesZd&#8217;]<br \/>\n&nbsp;<\/p>\n<h3>4.4 Repository Interface<\/h3>\n<p>The repository interface is used for extending the CRUD interface. This interface adds the layer of a repository in the program. Spring Data JPA provides two major ways of creating queries. These queries are then used in the repository interface to fetch the data from the database.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Repository class for extending CRUD repository<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.tutorial.repo;\n\nimport java.util.List;\n\nimport org.springframework.data.jpa.repository.Query;\nimport org.springframework.data.repository.CrudRepository;\nimport org.springframework.data.repository.query.Param;\nimport org.springframework.stereotype.Repository;\n\nimport com.tutorial.model.Employee;\n\n@Repository\npublic interface EmployeeRepository extends CrudRepository&lt;Employee, Long&gt;{\n\tList findByLastName(String lastName);\n\t\n@Query(\"SELECT e FROM Employee e WHERE e.age = :age\")\n    public List findByAge(@Param(\"age\") int age);\n}\n\n<\/pre>\n<p>The <code>CrudRepository<\/code> is the interface from SpringData Common project. The two methods mentioned above for query creation is used at the below-mentioned places in the code.<\/p>\n<p><strong>Automatic Custom Query:<\/strong><\/p>\n<pre class=\"brush:java\"> List findByLastName(String lastName);<\/pre>\n<p>The method <code>findByLastName<\/code> contains the last name as the parameter which will be used for the search of data. Also, the query will be automatically created using JPA query builder.<\/p>\n<p><strong>Manual Custom Query:<\/strong><\/p>\n<pre class=\"brush:java\"> @Query(\"SELECT e FROM Employee e WHERE e.age = :age\")\n    public List findByAge(@Param(\"age\") int age);<\/pre>\n<p>In this method we have manually defined a query to fetch the data based on age and the query is then bound with the <code>findByAge<\/code> method.<\/p>\n<h3>4.5 Properties file<\/h3>\n<p>As part of spring boot for connecting to the database, we will provide the details in the properties file. The properties file will look as shown below.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>application.properties file for the spring boot<\/em><\/span><\/p>\n<pre class=\"brush:bash\">spring.datasource.url=jdbc:postgresql:\/\/localhost:5432\/postgres\nspring.datasource.username=postgres\nspring.datasource.password=password\nspring.jpa.generate-ddl=true\nspring.jpa.show-sql=true\nspring.jpa.properties.hibernate.format_sql=true\n<\/pre>\n<p>As part of the properties file, the database URL and credentials are provided. The property <code>spring.jpa.show-sql=true<\/code> shows the SQL query generated during the data manipulation by JPA.<\/p>\n<h3>4.6 Controller class<\/h3>\n<p>The controller is the most important class of the complete program. This is the class responsible for all the url mapping. We have added the repository methods for data manipulation in this class itself.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Controller class for the program<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.tutorial.controller;\n\nimport java.util.List;\nimport java.util.Optional;\n\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.http.HttpStatus;\nimport org.springframework.web.bind.annotation.RequestBody;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RequestMethod;\nimport org.springframework.web.bind.annotation.RequestParam;\nimport org.springframework.web.bind.annotation.RestController;\n\nimport com.tutorial.model.Employee;\nimport com.tutorial.repo.EmployeeRepository;\n\n@RestController\n@RequestMapping(\"\/employee\")\npublic class WebController {\n\t@Autowired\n\tEmployeeRepository repository;\n\t\n\t@RequestMapping(value=\"\/save\",method = RequestMethod.POST)\n\tpublic HttpStatus insertEmployee(@RequestBody Employee employee){\n\t\tboolean status = repository.save(employee) != null;\t\t\n\t\treturn status? HttpStatus.CREATED : HttpStatus.BAD_REQUEST;\n\t}\n\t\n\t\n\t@RequestMapping(\"\/findall\")\n\tpublic List findAll(){\n\t\t\n\t\t\n\t\treturn (List) repository.findAll();\n\t}\n\t\n\t@RequestMapping(\"\/findbyid\")\n\tpublic Optional findById(@RequestParam(\"id\") long id){\n\t\tOptional result = repository.findById(id);\n\t\treturn result;\n\t}\n\t\n\t@RequestMapping(\"\/findbylastname\")\n\tpublic List fetchDataByLastName(@RequestParam(\"lastname\") String lastName){\n\t\t \t\t\n\t\treturn repository.findByLastName(lastName);\n\t}\n\t@RequestMapping(\"\/findbyage\")\n\tpublic List fetchDataByAge(@RequestParam(\"age\") int age){\n\t\t \t\t\n\t\treturn repository.findByAge(age);\n\t}\n}\n<\/pre>\n<p><code>@RestController<\/code> tag is used for defining the class as a rest controller class.<br \/>\n<code>@RequestMapping<\/code> tag specifies the path mapping for the request. Value attribute specifies the URL mapping and the method attributes specifies the type of method it is like GET, POST, PUT, etc.<br \/>\nIf we don&#8217;t specify the method attribute by default the method is considered as GET method.<\/p>\n<p>In this class, we have autowired the repository and are using the methods available in the interface for data retrieval, insertion and deletion.<\/p>\n<p>For running the spring boot application we have to provide the command <code>spring-boot: run<\/code> .<\/p>\n<h2>5. Output<\/h2>\n<p>Below mentioned queries can be used for the creation of Employee table in PostgreSQL.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>SQL query for Employee table creation<\/em><\/span><\/p>\n<pre class=\"brush:sql\">create table employee (\nid serial not null primary key,\nfirstName varchar(20) not null,\nlastName varchar(20) not null,\nage integer not null\n);<\/pre>\n<p>Lets us check the data available in the database. The current data available in the database is shown below.<\/p>\n<p><figure id=\"attachment_76218\" aria-describedby=\"caption-attachment-76218\" style=\"width: 645px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Result_JCG.jpg\"><img decoding=\"async\" class=\"size-full wp-image-76218\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Result_JCG.jpg\" alt=\"\" width=\"645\" height=\"185\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Result_JCG.jpg 645w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Result_JCG-300x86.jpg 300w\" sizes=\"(max-width: 645px) 100vw, 645px\" \/><\/a><figcaption id=\"caption-attachment-76218\" class=\"wp-caption-text\">Data in PostgreSQL<\/figcaption><\/figure><\/p>\n<p>Now let&#8217;s try the findall\u00a0API using postman collection to get the rows as result. The result will look as shown below on postman.<\/p>\n<p><figure id=\"attachment_76219\" aria-describedby=\"caption-attachment-76219\" style=\"width: 728px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Findall_JCG.jpg\"><img decoding=\"async\" class=\"size-full wp-image-76219\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Findall_JCG.jpg\" alt=\"\" width=\"728\" height=\"709\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Findall_JCG.jpg 728w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Findall_JCG-300x292.jpg 300w\" sizes=\"(max-width: 728px) 100vw, 728px\" \/><\/a><figcaption id=\"caption-attachment-76219\" class=\"wp-caption-text\">Result of findall API on postman<\/figcaption><\/figure><\/p>\n<p>In order to add some employee object in a database. We will use the save API and will pass an employee object.<\/p>\n<p><figure id=\"attachment_76283\" aria-describedby=\"caption-attachment-76283\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Save_JCG-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-76283\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Save_JCG-1.jpg\" alt=\"\" width=\"810\" height=\"384\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Save_JCG-1.jpg 810w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Save_JCG-1-300x142.jpg 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/Save_JCG-1-768x364.jpg 768w\" sizes=\"(max-width: 810px) 100vw, 810px\" \/><\/a><figcaption id=\"caption-attachment-76283\" class=\"wp-caption-text\">Object storage in DB with save API<\/figcaption><\/figure><\/p>\n<p>Let&#8217;s check the database post the save API usage. The database will look as shown below.<\/p>\n<p><figure id=\"attachment_76227\" aria-describedby=\"caption-attachment-76227\" style=\"width: 591px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/AfterSave_JCG.jpg\"><img decoding=\"async\" class=\"size-full wp-image-76227\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/AfterSave_JCG.jpg\" alt=\"\" width=\"591\" height=\"205\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/AfterSave_JCG.jpg 591w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/AfterSave_JCG-300x104.jpg 300w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/a><figcaption id=\"caption-attachment-76227\" class=\"wp-caption-text\">After the data is saved using Save API<\/figcaption><\/figure><\/p>\n<p>Similarly, we can use the <code>findbylastname<\/code> API for finding the records having the last name provided in the request parameter.<\/p>\n<p><figure id=\"attachment_76228\" aria-describedby=\"caption-attachment-76228\" style=\"width: 770px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/findbylastname_JCG.jpg\"><img decoding=\"async\" class=\"size-full wp-image-76228\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/findbylastname_JCG.jpg\" alt=\"\" width=\"770\" height=\"749\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/findbylastname_JCG.jpg 770w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/findbylastname_JCG-300x292.jpg 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/findbylastname_JCG-768x747.jpg 768w\" sizes=\"(max-width: 770px) 100vw, 770px\" \/><\/a><figcaption id=\"caption-attachment-76228\" class=\"wp-caption-text\">Result of findbylastname API<\/figcaption><\/figure><\/p>\n<p>Let&#8217;s see the result for the method which is bound to manual custom query.<\/p>\n<p><figure id=\"attachment_76510\" aria-describedby=\"caption-attachment-76510\" style=\"width: 530px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/05\/findbyage_JCG.jpg\"><img decoding=\"async\" class=\"size-full wp-image-76510\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/05\/findbyage_JCG.jpg\" alt=\"\" width=\"530\" height=\"738\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/05\/findbyage_JCG.jpg 530w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/05\/findbyage_JCG-215x300.jpg 215w\" sizes=\"(max-width: 530px) 100vw, 530px\" \/><\/a><figcaption id=\"caption-attachment-76510\" class=\"wp-caption-text\">Result of findByAge API<\/figcaption><\/figure><\/p>\n<h2>6. Summary<\/h2>\n<p>The summary of the tutorial is mentioned below.<\/p>\n<ol>\n<li>SpringData JPA provides repository abstraction which is used for JPA persistence APIs.<\/li>\n<li>We learned about the usage of CRUD repository.<\/li>\n<li>We used automatic custom queries for searching the rows based on the last name.<\/li>\n<li>We learned about manual custom query creation.<\/li>\n<li>The JPA exception translation is dependent on <code>@Repository<\/code> tag.<\/li>\n<\/ol>\n<h2>7. Download the Eclipse project<\/h2>\n<p>This was a tutorial for SpringData JPA with SpringBoot.<\/p>\n<div class=\"download\">You can download the full source code of this example here: <strong><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/05\/SpringJPA-PostgreSQL.zip\">SpringJPA-PostgreSQL.zip<\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Managing data between java classes or objects and the relational database is a very cumbersome and tricky task. The DAO layer usually contains a lot of boilerplate code that should be simplified in order to reduce the number of lines of code and make the code reusable. In this tutorial, we will discuss spring data&#8217;s &hellip;<\/p>\n","protected":false},"author":22230,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[33,321],"class_list":["post-75813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-jpa","tag-spring-data"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring Data JPA Tutorial - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Managing data between java classes or objects and the relational database is a very cumbersome and tricky task. The DAO layer usually contains a lot of\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Data JPA Tutorial - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Managing data between java classes or objects and the relational database is a very cumbersome and tricky task. The DAO layer usually contains a lot of\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-05-02T22:00:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-11T08:47:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Anand Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Anand Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html\"},\"author\":{\"name\":\"Anand Kumar\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/69738684d820603e2d7199877fcab97e\"},\"headline\":\"Spring Data JPA Tutorial\",\"datePublished\":\"2018-05-02T22:00:11+00:00\",\"dateModified\":\"2023-12-11T08:47:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html\"},\"wordCount\":1903,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"JPA\",\"Spring Data\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html\",\"name\":\"Spring Data JPA Tutorial - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2018-05-02T22:00:11+00:00\",\"dateModified\":\"2023-12-11T08:47:52+00:00\",\"description\":\"Managing data between java classes or objects and the relational database is a very cumbersome and tricky task. The DAO layer usually contains a lot of\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/05\\\/spring-data-jpa-tutorial.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Spring Data JPA Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/69738684d820603e2d7199877fcab97e\",\"name\":\"Anand Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3c15ec93118950f8571d06f66acd10f962bf608f770ab21e4e81e5ba9c62241?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3c15ec93118950f8571d06f66acd10f962bf608f770ab21e4e81e5ba9c62241?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3c15ec93118950f8571d06f66acd10f962bf608f770ab21e4e81e5ba9c62241?s=96&d=mm&r=g\",\"caption\":\"Anand Kumar\"},\"description\":\"Anand has graduated from Kuvempu University, India in Electronics and Instrumentation department. He has over 8 years of IT experience and is mainly involved in design and programming for websites and server side logic using Java oriented technologies like spring-boot, spring-data, spring-security, Hibernate, etc. He has worked with start-up companies and small business setup to Large Multinational companies.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/anand-kumar-54b4a1b9\\\/ \"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/anand-kumar\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Data JPA Tutorial - Java Code Geeks","description":"Managing data between java classes or objects and the relational database is a very cumbersome and tricky task. The DAO layer usually contains a lot of","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html","og_locale":"en_US","og_type":"article","og_title":"Spring Data JPA Tutorial - Java Code Geeks","og_description":"Managing data between java classes or objects and the relational database is a very cumbersome and tricky task. The DAO layer usually contains a lot of","og_url":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2018-05-02T22:00:11+00:00","article_modified_time":"2023-12-11T08:47:52+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Anand Kumar","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Anand Kumar","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html"},"author":{"name":"Anand Kumar","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/69738684d820603e2d7199877fcab97e"},"headline":"Spring Data JPA Tutorial","datePublished":"2018-05-02T22:00:11+00:00","dateModified":"2023-12-11T08:47:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html"},"wordCount":1903,"commentCount":2,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["JPA","Spring Data"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html","url":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html","name":"Spring Data JPA Tutorial - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2018-05-02T22:00:11+00:00","dateModified":"2023-12-11T08:47:52+00:00","description":"Managing data between java classes or objects and the relational database is a very cumbersome and tricky task. The DAO layer usually contains a lot of","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2018\/05\/spring-data-jpa-tutorial.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Spring Data JPA Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/69738684d820603e2d7199877fcab97e","name":"Anand Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f3c15ec93118950f8571d06f66acd10f962bf608f770ab21e4e81e5ba9c62241?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f3c15ec93118950f8571d06f66acd10f962bf608f770ab21e4e81e5ba9c62241?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f3c15ec93118950f8571d06f66acd10f962bf608f770ab21e4e81e5ba9c62241?s=96&d=mm&r=g","caption":"Anand Kumar"},"description":"Anand has graduated from Kuvempu University, India in Electronics and Instrumentation department. He has over 8 years of IT experience and is mainly involved in design and programming for websites and server side logic using Java oriented technologies like spring-boot, spring-data, spring-security, Hibernate, etc. He has worked with start-up companies and small business setup to Large Multinational companies.","sameAs":["https:\/\/www.linkedin.com\/in\/anand-kumar-54b4a1b9\/ "],"url":"https:\/\/www.javacodegeeks.com\/author\/anand-kumar"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/75813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/22230"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=75813"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/75813\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=75813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=75813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=75813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}