{"id":74649,"date":"2018-03-21T16:00:29","date_gmt":"2018-03-21T14:00:29","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=74649"},"modified":"2023-12-11T10:38:48","modified_gmt":"2023-12-11T08:38:48","slug":"spring-data-jpa-example-with-spring-boot","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html","title":{"rendered":"Spring Data JPA Example with Spring Boot"},"content":{"rendered":"<h2>1. Introduction<\/h2>\n<p>In this post, we shall demonstrate how to leverage the powerful <a href=\"https:\/\/projects.spring.io\/spring-data-jpa\/\" target=\"_blank\" rel=\"noopener\">Spring Data JPA<\/a> APIs to interact with the database, in-memory <a href=\"http:\/\/www.h2database.com\/html\/main.html\" target=\"_blank\" rel=\"noopener\">H2 database<\/a> for this lesson.<\/p>\n<p>Spring Data JPA offers a set of very powerful and highly-abstracted interfaces which are used to interact with any underlying database. Databases can be MySQL, MongoDB, Elasticsearch or any other supported database. Other advantages for Spring Data JPA include:<\/p>\n<ul>\n<li>Support to build extended repositories based on JPA Convention<\/li>\n<li>In-built pagination support and dynamic query execution<\/li>\n<li>Support for XML based entity mapping<\/li>\n<\/ul>\n<p>In this example, we will make use of H2 in-memory database. The choice for the database should not affect the Spring Data definitions we will construct as this is the main advantage Spring Data JPA offers. It enables us to completely separate the Database queries from the application logic.<\/p>\n<h2>2. Project Setup<\/h2>\n<p>We will be using one of the many Maven archetypes to create a sample project for our example. To create the project execute the following command in a directory that you will use as workspace:<\/p>\n<pre class=\"brush:bash\">mvn archetype:generate -DgroupId=com.javacodegeeks.example -DartifactId=JCG-SpringDataJPA-example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false\n<\/pre>\n<p>If you are running maven for the first time, it will take a few seconds to accomplish the generate command because maven has to download all the required plugins and artifacts in order to make the generation task.<\/p>\n<p><figure id=\"attachment_75083\" aria-describedby=\"caption-attachment-75083\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-75083\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/data-jpa-project-setup.png\" alt=\"\" width=\"820\" height=\"421\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/data-jpa-project-setup.png 860w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/data-jpa-project-setup-300x154.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/data-jpa-project-setup-768x395.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><figcaption id=\"caption-attachment-75083\" class=\"wp-caption-text\">JPA Project Setup using Maven<\/figcaption><\/figure><\/p>\n<p>Notice that now, you will have a new directory with the same name as the <code>artifactId<\/code> inside the chosen directory. Now, feel free to open the project in your favourite IDE.<\/p>\n<p>Finally, instead of using an IDE to make this project, we used a simple maven command. This helps us to make project setup and initialisation free from any specific IDE you may use.<\/p>\n<h2>3. Maven Dependencies<\/h2>\n<p>To start with, we need to add appropriate Maven dependencies to our project. We will add the following dependency to our <em>pom.xml<\/em> file:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>pom.xml<\/em><\/span><\/p>\n<pre class=\"brush:xml\">&lt;parent&gt;\n  &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n  &lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\n  &lt;version&gt;1.5.10.RELEASE&lt;\/version&gt;\n  &lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt;\n&lt;\/parent&gt;\n\n&lt;properties&gt;\n  &lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\n  &lt;project.reporting.outputEncoding&gt;UTF-8&lt;\/project.reporting.outputEncoding&gt;\n  &lt;java.version&gt;1.8&lt;\/java.version&gt;\n&lt;\/properties&gt;\n\n&lt;dependencies&gt;\n\n  &lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;\/artifactId&gt;\n  &lt;\/dependency&gt;\n\n  &lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n  &lt;\/dependency&gt;\n\n  &lt;dependency&gt;\n    &lt;groupId&gt;com.h2database&lt;\/groupId&gt;\n    &lt;artifactId&gt;h2&lt;\/artifactId&gt;\n    &lt;scope&gt;runtime&lt;\/scope&gt;\n  &lt;\/dependency&gt;\n  \n  &lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt;\n    &lt;scope&gt;test&lt;\/scope&gt;\n  &lt;\/dependency&gt;\n  \n&lt;\/dependencies&gt;\n\n&lt;build&gt;\n  &lt;plugins&gt;\n    &lt;plugin&gt;\n      &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n      &lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;\n    &lt;\/plugin&gt;\n  &lt;\/plugins&gt;\n&lt;\/build&gt;\n<\/pre>\n<p>Find the latest Spring related dependencies <a href=\"http:\/\/search.maven.org\/#search%7Cga%7C1%7Cg%3A%22org.springframework.boot%22\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>Note that we have also added the H2 database dependency here as well with its scope as runtime as the H2 data is washed away as soon as the application has stopped. In this lesson, we will not focus on how H2 actually works but will restrict ourself to Spring Data JPA APIs. You may also see how we can <a href=\"https:\/\/www.javacodegeeks.com\/2017\/12\/configure-embedded-h2-console-spring-mvc-application.html\" target=\"_blank\" rel=\"noopener\">Configure Embedded H2 Console With a Spring Application<\/a>.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h2>4. Project Structure<\/h2>\n<p>Before we move on and start working on the code for the project, let&#8217;s present here the projet structure we will have once we&#8217;re finished adding all code to the project:<\/p>\n<p><figure id=\"attachment_74667\" aria-describedby=\"caption-attachment-74667\" style=\"width: 505px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"size-full wp-image-74667\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-data-jpa-project-structure-1.png\" alt=\"\" width=\"505\" height=\"363\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-data-jpa-project-structure-1.png 505w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-data-jpa-project-structure-1-300x216.png 300w\" sizes=\"(max-width: 505px) 100vw, 505px\" \/><figcaption id=\"caption-attachment-74667\" class=\"wp-caption-text\">Project Structure<\/figcaption><\/figure><\/p>\n<p>We have divided the project into multiple packages so that the principle of <a href=\"https:\/\/stackoverflow.com\/questions\/98734\/what-is-separation-of-concerns\" target=\"_blank\" rel=\"noopener\">separation of concern<\/a> is followed and code remains modular.<\/p>\n<h2>5. Defining the Model<\/h2>\n<p>We will start by adding a very simple model in our project, a <code>Person<\/code>. Its definition will be very standard, like:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Person.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">import javax.persistence.Entity;\nimport javax.persistence.GeneratedValue;\nimport javax.persistence.Id;\n\n@Entity\npublic class Person {\n\n    @Id\n    @GeneratedValue\n    private Long id;\n    private String name;\n    private int age;\n\n    public Person() {\n    }\n\n    public Person(String name, int age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    \/\/standard getters and setters\n\n    @Override\n    public String toString() {\n        return String.format(\"Person{id=%d, name='%s', age=%d}\", id, name, age);\n    }\n}\n<\/pre>\n<p>We omitted standard getters and setters for brevity but they are necessary to be made as Jackson uses them during <a href=\"https:\/\/www.javacodegeeks.com\/2013\/03\/serialization-in-java.html\" target=\"_blank\" rel=\"noopener\">Serialization and Deserialization<\/a> of an Object.<\/p>\n<p>The <code>@Entity<\/code> annotation marks this POJO as an object which will be managed by the Spring Data APIs and its fields will be treated as table columns (unless marked <a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/transient-variables-in-java\/\" target=\"_blank\" rel=\"noopener\">transient<\/a>).<\/p>\n<p>Finally, we added a custom implementation for the <code>toString()<\/code> method so that we can print related data when we test our application.<\/p>\n<h2>6. Defining JPA Repository<\/h2>\n<p>JPA provides us with a very simple way of defining a JPA Repository interface.<\/p>\n<p>Before getting to know how to define a JPA Repository, we need to remember that each JPA interface is only made to interact with a single Entity of Database Table when JPA-related functionality is leveraged. We will understand this deeply if we have a look at the interface definition:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>PersonRepository.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">import com.javacodegeeks.jpaexample.model.Person;\nimport org.springframework.data.jpa.repository.JpaRepository;\nimport org.springframework.stereotype.Repository;\n\n@Repository\npublic interface PersonRepository extends JpaRepository&lt;Person, Long&gt; {\n}\n<\/pre>\n<p>Although above interface definition is empty, we still have some points which we need to understand:<\/p>\n<ul>\n<li><code>@Repository<\/code> annotation marks this interface as a Spring Bean which is initialised on application startup. With this annotation, Spring takes care of managing exception database interaction throws gracefuly<\/li>\n<li>We used <code>Person<\/code> as a parameter to signify that this JPA interface will manage the <code>Person<\/code> Entity<\/li>\n<li>Finally, we also passed the data type <code>Long<\/code> as a parameter. This signifies that the <code>Person<\/code> Entity contains a unique identifier which is of the type <code>Long<\/code><\/li>\n<\/ul>\n<h2>7. Making Service interface<\/h2>\n<p>In this section, we will define a service interface which will act as a contract for the implementation and represnet all the actions our Service must support. These actions will be related to making a new user and getting information related to the objects in database.<\/p>\n<p>Here is the contract definition we will be using:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>PersonService.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">import com.javacodegeeks.jpaexample.model.Person;\nimport java.util.List;\n\npublic interface PersonService {\n\n    Person createPerson(Person person);\n    Person getPerson(Long id);\n    Person editPerson(Person person);\n    void deletePerson(Person person);\n    void deletePerson(Long id);\n    List getAllPersons(int pageNumber, int pageSize);\n    List getAllPersons();\n    long countPersons();\n}\n<\/pre>\n<p>We have mentioned all four CRUD operations in this contract along with concept of Pagination.<\/p>\n<p>A paginated API is important to make when we introduce getting all objects from the database based on a <code>pageSize<\/code> and a <code>pageNumber<\/code>. The <code>pageSize<\/code> attribute signifies the number of objects which are fetched from the database whereas the <code>pageNumber<\/code> attribute act as <strong>skip<\/strong> part of the query. For detailed lesson on how Pagination works in Spring, read <a href=\"https:\/\/www.javacodegeeks.com\/2013\/01\/spring-data-jpa-and-pagination.html\" target=\"_blank\" rel=\"noopener\">this lesson<\/a>.<br \/>\n[ulp id=&#8217;c4qGwqz2zZ7CesZd&#8217;]<br \/>\n&nbsp;<\/p>\n<h2>8. Providing Service implementation<\/h2>\n<p>We will use the above interface definition to provide its implementation so that we can perform CRUD operations related to the <code>Person<\/code> Entity we defined earlier. We will do it here:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>PersonServiceImpl.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">import com.javacodegeeks.jpaexample.model.Person;\nimport com.javacodegeeks.jpaexample.repository.PersonRepository;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.data.domain.PageRequest;\nimport org.springframework.stereotype.Service;\n\nimport java.util.List;\n\n@Service\npublic class PersonServiceImpl implements PersonService {\n\n    @Autowired\n    private PersonRepository personRepository;\n\n    @Override\n    public Person createPerson(Person person) {\n        return personRepository.save(person);\n    }\n\n    @Override\n    public Person getPerson(Long id) {\n        return personRepository.findOne(id);\n    }\n\n    @Override\n    public Person editPerson(Person person) {\n        return personRepository.save(person);\n    }\n\n    @Override\n    public void deletePerson(Person person) {\n        personRepository.delete(person);\n    }\n\n    @Override\n    public void deletePerson(Long id) {\n        personRepository.delete(id);\n    }\n\n    @Override\n    public List&lt;Person&gt; getAllPersons(int pageNumber, int pageSize) {\n        return personRepository.findAll(new PageRequest(pageNumber, pageSize)).getContent();\n    }\n\n    @Override\n    public List&lt;Person&gt; getAllPersons() {\n        return personRepository.findAll();\n    }\n\n    @Override\n    public long countPersons() {\n        return personRepository.count();\n    }\n}\n<\/pre>\n<p>It is positively surprising that all the method implementations are only one line of code. This shows the level of abstraction JPA Repositories provide to us.<\/p>\n<p>Most of the operations above are simple to understand. Main thing to notice is that we never provided any methods in the Repository we made like <code>getAllPersons()<\/code> etc! Then how did these methods appeared altogether? The answer, again, lies in the abstraction JPA Repositories provide to us. All of the methods like <code>findAll()<\/code>, <code>delete()<\/code>, <code>save(...)<\/code> etc. are in-built into the <code>JpaRepository<\/code> we extended in our repository interface definition.<\/p>\n<h2>9. Using the CommandLineRunner<\/h2>\n<p>To test all the code we have written toll now, along with the database interactionpart, we will be using the <code>CommandLineRunner<\/code> in our main class of our Spring Boot application. A <code>CommandLineRunner<\/code> runs right before the <code>main()<\/code> method for the Spring Boot application is called and so, it is an ideal space to perform any init steps or testing code.<\/p>\n<p>To test the application, we will use a service bean to perform database operations in our class:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>pom.xml<\/em><\/span><\/p>\n<pre class=\"brush:java\">import com.javacodegeeks.jpaexample.model.Person;\nimport com.javacodegeeks.jpaexample.service.PersonService;\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.CommandLineRunner;\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\n\n@SpringBootApplication\npublic class DataJpaApp implements CommandLineRunner {\n\n  private static final Logger LOG = LoggerFactory.getLogger(\"JCG\");\n\n  @Autowired\n  private PersonService service;\n\n  public static void main(String[] args) {\n    SpringApplication.run(DataJpaApp.class, args);\n  }\n\n  @Override\n  public void run(String... strings) {\n\n    LOG.info(\"Current objects in DB: {}\", service.countPersons());\n\n    Person person = service.createPerson(new Person(\"Shubham\", 23));\n    LOG.info(\"Person created in DB : {}\", person);\n\n    LOG.info(\"Current objects in DB: {}\", service.countPersons());\n\n    person.setName(\"Programmer\");\n    Person editedPerson = service.editPerson(person);\n    LOG.info(\"Person edited in DB  : {}\", person);\n\n    service.deletePerson(person);\n    LOG.info(\"After deletion, count: {}\", service.countPersons());\n  }\n}\n<\/pre>\n<p>In above sample code, we just made simple calls to some important methods we created in our service like creating some data and accessing it later method calls.<\/p>\n<p>Now, we will finally run our project using Maven itself (again being independent from any IDE to run our project).<\/p>\n<h2>10. Running the Application<\/h2>\n<p>Running the application is easy with maven, just use the following command:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Running the Application<\/em><\/span><\/p>\n<pre class=\"brush:bash\">mvn spring-boot:run\n<\/pre>\n<p>Once we run the project, we will be seeing the following output:<\/p>\n<p><figure id=\"attachment_75084\" aria-describedby=\"caption-attachment-75084\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-75084\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/data-jpa-run-project.png\" alt=\"\" width=\"820\" height=\"167\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/data-jpa-run-project.png 860w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/data-jpa-run-project-300x61.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/data-jpa-run-project-768x156.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><figcaption id=\"caption-attachment-75084\" class=\"wp-caption-text\">Data JPA Project Output<\/figcaption><\/figure><\/p>\n<p>As expected, we first created some sample data and confirmed it by calling the <code>count()<\/code> method call. Finally, we deleted the data and again confirmed it with the <code>count()<\/code> method call.<\/p>\n<h2>11. Download the Source Code<\/h2>\n<p>This was an example of with Spring Boot and Spring Data JPA APIs along with the in-memory H2 database.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <strong><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/JCG-SpringDataJPA-Example.zip\">Spring Data JPA Example<\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction In this post, we shall demonstrate how to leverage the powerful Spring Data JPA APIs to interact with the database, in-memory H2 database for this lesson. Spring Data JPA offers a set of very powerful and highly-abstracted interfaces which are used to interact with any underlying database. Databases can be MySQL, MongoDB, Elasticsearch &hellip;<\/p>\n","protected":false},"author":20016,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[33,854],"class_list":["post-74649","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-jpa","tag-spring-boot"],"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 Example with Spring Boot - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"1. Introduction In this post, we shall demonstrate how to leverage the powerful Spring Data JPA APIs to interact with the database, in-memory H2 database\" \/>\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\/03\/spring-data-jpa-example-with-spring-boot.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 Example with Spring Boot - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"1. Introduction In this post, we shall demonstrate how to leverage the powerful Spring Data JPA APIs to interact with the database, in-memory H2 database\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.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-03-21T14:00:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-11T08:38:48+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=\"Shubham Aggarwal\" \/>\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=\"Shubham Aggarwal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-jpa-example-with-spring-boot.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-jpa-example-with-spring-boot.html\"},\"author\":{\"name\":\"Shubham Aggarwal\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/0953481a8babbb7a63907edb41f357ad\"},\"headline\":\"Spring Data JPA Example with Spring Boot\",\"datePublished\":\"2018-03-21T14:00:29+00:00\",\"dateModified\":\"2023-12-11T08:38:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-jpa-example-with-spring-boot.html\"},\"wordCount\":1212,\"commentCount\":9,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-jpa-example-with-spring-boot.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"JPA\",\"Spring Boot\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-jpa-example-with-spring-boot.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-jpa-example-with-spring-boot.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-jpa-example-with-spring-boot.html\",\"name\":\"Spring Data JPA Example with Spring Boot - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-jpa-example-with-spring-boot.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-jpa-example-with-spring-boot.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2018-03-21T14:00:29+00:00\",\"dateModified\":\"2023-12-11T08:38:48+00:00\",\"description\":\"1. Introduction In this post, we shall demonstrate how to leverage the powerful Spring Data JPA APIs to interact with the database, in-memory H2 database\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-jpa-example-with-spring-boot.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-jpa-example-with-spring-boot.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-data-jpa-example-with-spring-boot.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\\\/03\\\/spring-data-jpa-example-with-spring-boot.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 Example with Spring Boot\"}]},{\"@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\\\/0953481a8babbb7a63907edb41f357ad\",\"name\":\"Shubham Aggarwal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3f2c210fd210e1cafb930887d5f4c29613eb8183e62743a99e0cb93dcaec1a2b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3f2c210fd210e1cafb930887d5f4c29613eb8183e62743a99e0cb93dcaec1a2b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3f2c210fd210e1cafb930887d5f4c29613eb8183e62743a99e0cb93dcaec1a2b?s=96&d=mm&r=g\",\"caption\":\"Shubham Aggarwal\"},\"description\":\"Shubham is a Java EE Engineer with about 3 years of experience in building quality products with Spring Boot, Spring Data, AWS, Kafka, PrestoDB.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sbmaggarwal\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/shubham-aggarwal\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Data JPA Example with Spring Boot - Java Code Geeks","description":"1. Introduction In this post, we shall demonstrate how to leverage the powerful Spring Data JPA APIs to interact with the database, in-memory H2 database","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\/03\/spring-data-jpa-example-with-spring-boot.html","og_locale":"en_US","og_type":"article","og_title":"Spring Data JPA Example with Spring Boot - Java Code Geeks","og_description":"1. Introduction In this post, we shall demonstrate how to leverage the powerful Spring Data JPA APIs to interact with the database, in-memory H2 database","og_url":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2018-03-21T14:00:29+00:00","article_modified_time":"2023-12-11T08:38:48+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":"Shubham Aggarwal","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Shubham Aggarwal","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html"},"author":{"name":"Shubham Aggarwal","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/0953481a8babbb7a63907edb41f357ad"},"headline":"Spring Data JPA Example with Spring Boot","datePublished":"2018-03-21T14:00:29+00:00","dateModified":"2023-12-11T08:38:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html"},"wordCount":1212,"commentCount":9,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["JPA","Spring Boot"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html","url":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html","name":"Spring Data JPA Example with Spring Boot - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2018-03-21T14:00:29+00:00","dateModified":"2023-12-11T08:38:48+00:00","description":"1. Introduction In this post, we shall demonstrate how to leverage the powerful Spring Data JPA APIs to interact with the database, in-memory H2 database","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-data-jpa-example-with-spring-boot.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\/03\/spring-data-jpa-example-with-spring-boot.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 Example with Spring Boot"}]},{"@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\/0953481a8babbb7a63907edb41f357ad","name":"Shubham Aggarwal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3f2c210fd210e1cafb930887d5f4c29613eb8183e62743a99e0cb93dcaec1a2b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3f2c210fd210e1cafb930887d5f4c29613eb8183e62743a99e0cb93dcaec1a2b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3f2c210fd210e1cafb930887d5f4c29613eb8183e62743a99e0cb93dcaec1a2b?s=96&d=mm&r=g","caption":"Shubham Aggarwal"},"description":"Shubham is a Java EE Engineer with about 3 years of experience in building quality products with Spring Boot, Spring Data, AWS, Kafka, PrestoDB.","sameAs":["https:\/\/www.linkedin.com\/in\/sbmaggarwal\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/shubham-aggarwal"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/74649","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\/20016"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=74649"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/74649\/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=74649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=74649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=74649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}