{"id":76169,"date":"2018-04-28T15:00:46","date_gmt":"2018-04-28T12:00:46","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=76169"},"modified":"2023-12-11T12:13:57","modified_gmt":"2023-12-11T10:13:57","slug":"spring-hibernate-tutorial","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html","title":{"rendered":"Spring Hibernate Tutorial"},"content":{"rendered":"<h2>1. Introduction<\/h2>\n<p>In this post, we shall demonstrate how to leverage the power of one of the most popular ORM (object-relational mapping) tool, <a href=\"https:\/\/www.javacodegeeks.com\/2015\/03\/hibernate-tutorial.html\" target=\"_blank\" rel=\"noopener\">Hibernate<\/a> which facilitates the conversion of an object-oriented domain model to a traditional relational database. Hibernate is one of the most popular Java frameworks out there. For this reason we have provided an abundance of tutorials here at Java Code Geeks, most of which can be found <a href=\"https:\/\/www.javacodegeeks.com\/tutorials\/java-tutorials\/enterprise-java-tutorials#Hibernate\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>In this lesson, we will create a simple Spring Boot based application which will leverage the power of Hibernate configuration along with Spring Data JPA. 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 Hibernate &amp; Spring Data JPA offers. It enables us to completely separate the Database queries from the application logic.<\/p>\n<h2>2. Making the Spring Boot Project<\/h2>\n<p>We will be using one of the most popular web tool to make a sample project in this lesson and won&#8217;t do it from the command line, we will make use of <a href=\"https:\/\/start.spring.io\/\" target=\"_blank\" rel=\"noopener\">Spring Initializr<\/a>. Just open the link in your browser and explore around. To setup our project, we used the following configuration:<\/p>\n<p><figure id=\"attachment_76340\" aria-describedby=\"caption-attachment-76340\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/project-creation-1.png\"><img decoding=\"async\" class=\"size-full wp-image-76340\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/project-creation-1.png\" alt=\"Spring Initializr config\" width=\"820\" height=\"973\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/project-creation-1.png 820w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/project-creation-1-253x300.png 253w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/project-creation-1-768x911.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-76340\" class=\"wp-caption-text\">Spring Initializr config<\/figcaption><\/figure><\/p>\n<p>We added three dependencies in this tool:<\/p>\n<ul>\n<li><strong>Web<\/strong>: This is a basic Spring dependency which collects configuration related and basic annotations into the project.<\/li>\n<li><strong>H2<\/strong>: As we make use of an in-memory database, this dependency is required.<\/li>\n<li><strong>Data JPA<\/strong>: We will make use of Spring Data JPA for our Data Access layer.<\/li>\n<\/ul>\n<p>Next, unzip the downloaded zip project and import it into your favourite IDE.<\/p>\n<h2>3. Maven Dependencies<\/h2>\n<p>To start with, we need to see what Maven dependencies were added to our project by the tool and what others are needed. We will have 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;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;org.hibernate&lt;\/groupId&gt;\n    &lt;artifactId&gt;hibernate-search-orm&lt;\/artifactId&gt;\n    &lt;version&gt;5.6.1.Final&lt;\/version&gt;\n&lt;\/dependency&gt;\n\n&lt;dependency&gt;\n    &lt;groupId&gt;org.hibernate&lt;\/groupId&gt;\n    &lt;artifactId&gt;hibernate-entitymanager&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<\/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>. We have added dependencies which are need to perform Hibernate search too.<\/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 Hibernate configuration. 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>.<\/p>\n<p>Finally, to understand all the JARs which are added to the project when we added this dependency, we can run a simple Maven command which allows us to see a complete Dependency Tree for a project when we add some dependencies to it. Here is a command which we can use:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Check Dependency Tree<\/em><\/span><\/p>\n<pre class=\"brush:bash\">mvn dependency:tree\n<\/pre>\n<p>When we run this command, it will show us the following Dependency Tree:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Dependency Tree<\/em><\/span><\/p>\n<pre class=\"brush:bash\">[INFO] --------&lt; com.javacodegeeks.example:JCG-BootHibernate-Example &gt;---------\n[INFO] Building JCG-BootHibernate-Example 1.0-SNAPSHOT\n[INFO] --------------------------------[ jar ]---------------------------------\n[INFO]\n[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ JCG-BootHibernate-Example ---\n[INFO] com.javacodegeeks.example:JCG-BootHibernate-Example:jar:1.0-SNAPSHOT\n[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.5.6.RELEASE:compile\n[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.5.6.RELEASE:compile\n[INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.5.6.RELEASE:compile\n[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.6.RELEASE:compile\n[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.5.6.RELEASE:compile\n[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.11:compile\n[INFO] |  |  |  |  \\- ch.qos.logback:logback-core:jar:1.1.11:compile\n[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.25:compile\n[INFO] |  |  |  \\- org.slf4j:log4j-over-slf4j:jar:1.7.25:compile\n[INFO] |  |  \\- org.yaml:snakeyaml:jar:1.17:runtime\n[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:1.5.6.RELEASE:compile\n[INFO] |  |  +- org.springframework:spring-aop:jar:4.3.10.RELEASE:compile\n[INFO] |  |  \\- org.aspectj:aspectjweaver:jar:1.8.10:compile\n[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.5.6.RELEASE:compile\n[INFO] |  |  +- org.apache.tomcat:tomcat-jdbc:jar:8.5.16:compile\n[INFO] |  |  |  \\- org.apache.tomcat:tomcat-juli:jar:8.5.16:compile\n[INFO] |  |  \\- org.springframework:spring-jdbc:jar:4.3.10.RELEASE:compile\n[INFO] |  +- org.hibernate:hibernate-core:jar:5.0.12.Final:compile\n[INFO] |  |  +- antlr:antlr:jar:2.7.7:compile\n[INFO] |  |  \\- org.jboss:jandex:jar:2.0.0.Final:compile\n[INFO] |  +- javax.transaction:javax.transaction-api:jar:1.2:compile\n[INFO] |  +- org.springframework.data:spring-data-jpa:jar:1.11.6.RELEASE:compile\n[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:1.13.6.RELEASE:compile\n[INFO] |  |  +- org.springframework:spring-orm:jar:4.3.10.RELEASE:compile\n[INFO] |  |  +- org.springframework:spring-context:jar:4.3.10.RELEASE:compile\n[INFO] |  |  +- org.springframework:spring-tx:jar:4.3.10.RELEASE:compile\n[INFO] |  |  +- org.springframework:spring-beans:jar:4.3.10.RELEASE:compile\n[INFO] |  |  +- org.slf4j:slf4j-api:jar:1.7.25:compile\n[INFO] |  |  \\- org.slf4j:jcl-over-slf4j:jar:1.7.25:compile\n[INFO] |  \\- org.springframework:spring-aspects:jar:4.3.10.RELEASE:compile\n[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.6.RELEASE:compile\n[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.6.RELEASE:compile\n[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.16:compile\n[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.16:compile\n[INFO] |  |  \\- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.16:compile\n[INFO] |  +- org.hibernate:hibernate-validator:jar:5.3.5.Final:compile\n[INFO] |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile\n[INFO] |  |  \\- com.fasterxml:classmate:jar:1.3.3:compile\n[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.9:compile\n[INFO] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile\n[INFO] |  |  \\- com.fasterxml.jackson.core:jackson-core:jar:2.8.9:compile\n[INFO] |  +- org.springframework:spring-web:jar:4.3.10.RELEASE:compile\n[INFO] |  \\- org.springframework:spring-webmvc:jar:4.3.10.RELEASE:compile\n[INFO] |     \\- org.springframework:spring-expression:jar:4.3.10.RELEASE:compile\n[INFO] +- org.hibernate:hibernate-search-orm:jar:5.6.1.Final:compile\n[INFO] |  \\- org.hibernate:hibernate-search-engine:jar:5.6.1.Final:compile\n[INFO] |     +- org.apache.lucene:lucene-core:jar:5.5.4:compile\n[INFO] |     +- org.apache.lucene:lucene-misc:jar:5.5.4:compile\n[INFO] |     +- org.apache.lucene:lucene-analyzers-common:jar:5.5.4:compile\n[INFO] |     \\- org.apache.lucene:lucene-facet:jar:5.5.4:compile\n[INFO] |        \\- org.apache.lucene:lucene-queries:jar:5.5.4:compile\n[INFO] +- org.hibernate:hibernate-entitymanager:jar:5.0.12.Final:compile\n[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.3.1.Final:compile\n[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile\n[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final:compile\n[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile\n[INFO] |  +- org.javassist:javassist:jar:3.21.0-GA:compile\n[INFO] |  \\- org.apache.geronimo.specs:geronimo-jta_1.1_spec:jar:1.1.1:compile\n[INFO] +- com.h2database:h2:jar:1.4.196:runtime\n[INFO] \\- org.springframework.boot:spring-boot-starter-test:jar:1.5.6.RELEASE:test\n[INFO]    +- org.springframework.boot:spring-boot-test:jar:1.5.6.RELEASE:test\n[INFO]    +- org.springframework.boot:spring-boot-test-autoconfigure:jar:1.5.6.RELEASE:test\n[INFO]    +- com.jayway.jsonpath:json-path:jar:2.2.0:test\n[INFO]    |  \\- net.minidev:json-smart:jar:2.2.1:test\n[INFO]    |     \\- net.minidev:accessors-smart:jar:1.1:test\n[INFO]    |        \\- org.ow2.asm:asm:jar:5.0.3:test\n[INFO]    +- junit:junit:jar:4.12:test\n[INFO]    +- org.assertj:assertj-core:jar:2.6.0:test\n[INFO]    +- org.mockito:mockito-core:jar:1.10.19:test\n[INFO]    |  \\- org.objenesis:objenesis:jar:2.1:test\n[INFO]    +- org.hamcrest:hamcrest-core:jar:1.3:test\n[INFO]    +- org.hamcrest:hamcrest-library:jar:1.3:test\n[INFO]    +- org.skyscreamer:jsonassert:jar:1.4.0:test\n[INFO]    |  \\- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test\n[INFO]    +- org.springframework:spring-core:jar:4.3.10.RELEASE:compile\n[INFO]    \\- org.springframework:spring-test:jar:4.3.10.RELEASE:test\n[INFO] ------------------------------------------------------------------------\n[INFO] BUILD SUCCESS\n[INFO] ------------------------------------------------------------------------\n<\/pre>\n<p>Noticed something? So many dependencies were added by just adding four dependencies to the project. Spring Boot collects all related dependencies itself and leaves nothing for us in that matter. The biggest advantage is that all these dependencies are guaranteed to be compatible with each other.<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\u2019s present here the project structure we will have once we\u2019re finished adding all code to the project:<\/p>\n<p><figure id=\"attachment_76344\" aria-describedby=\"caption-attachment-76344\" style=\"width: 556px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/project-structure-1.png\"><img decoding=\"async\" class=\"size-full wp-image-76344\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/project-structure-1.png\" alt=\"Hibernate Project Structure\" width=\"556\" height=\"593\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/project-structure-1.png 556w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/project-structure-1-281x300.png 281w\" sizes=\"(max-width: 556px) 100vw, 556px\" \/><\/a><figcaption id=\"caption-attachment-76344\" class=\"wp-caption-text\">Hibernate 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<p>Note that the <em>indexpath<\/em> directory was created by Hibernate for storing indexes (discussed later in the lesson) and it will not exist when you import the project in your IDE.<\/p>\n<h2>5. Defining Hibernate Dialects<\/h2>\n<p>In the application.properties file, we define two properties which are used by Spring Data JPA which is present on the project classpath. Spring Boot uses Hibernate as the default JPA implementation.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>application.properties<\/em><\/span><\/p>\n<pre class=\"brush:bash\">## Hibernate Properties\n# The SQL dialect makes Hibernate generate better SQL for the chosen database\nspring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect\n\n# Hibernate DDL auto (create, create-drop, validate, update)\nspring.jpa.hibernate.ddl-auto = update\n<\/pre>\n<p>The <code>spring.jpa.hibernate.ddl-auto<\/code> property is important here. Due to this, Spring Data will automatically make Database tables on the basis of entities we define in our project and columns will be made from the entity&#8217;s fields. As we have set the property to <code>update<\/code>, whenever we update a field in our Entity class, it will automatically be updated in the DB as soon as we relaunch the project.<\/p>\n<h2>6. Defining an Entity<\/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\">@Entity\n@Indexed\npublic class Person {\n\n    @Id\n    @GeneratedValue\n    private Long id;\n\n    @Field(termVector = TermVector.YES)\n    private String name;\n\n    @Field\n    private int age;\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 transient) and <code>@Field<\/code> annotation marks that this field should be indexed by Hibernate so that we can run the<strong> full-text search<\/strong> query on these fields as well.<\/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>7. Making a Service Interface<\/h2>\n<p>In this section, we will define a service interface which will act as a contract for the implementation and represent 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 the 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\">public interface PersonService {\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&lt;Person&gt; getAllPersons(int pageNumber, int pageSize);\n    List&lt;Person&gt; getAllPersons();\n    long countPersons();\n    List&lt;Person&gt; fuzzySearchPerson(String term);\n    List&lt;Person&gt; wildCardSearchPerson(String term);\n}\n<\/pre>\n<p>Notice that the contract also includes two methods at the end to provide support for Hibernate search as well.<br \/>\n[ulp id=&#8217;oiCtu6x3AwpaVNPI&#8217;]<br \/>\n&nbsp;<\/p>\n<h2>8. Implementing the Service<\/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\">@Service\npublic class PersonServiceImpl implements PersonService {\n\n    private final PersonRepository personRepository;\n    private final PersonDAL personDAL;\n\n    @Autowired\n    public PersonServiceImpl(PersonRepository personRepository, PersonDAL personDAL) {\n        this.personRepository = personRepository;\n        this.personDAL = personDAL;\n    }\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    @Override\n    @Transactional(readOnly = true)\n    public List&lt;Person&gt; fuzzySearchPerson(String term) {\n        return personDAL.fuzzySearchPerson(term);\n    }\n\n    @Override\n    @Transactional(readOnly = true)\n    public List&lt;Person&gt; wildCardSearchPerson(String term) {\n        return personDAL.wildCardSearchPerson(term);\n    }\n}\n<\/pre>\n<p>We just used the DAL bean to access the methods we defined above. We also made use of the\u00a0<code>@Transactional(readOnly = true)<\/code> annotation so that we don&#8217;t have to open a Hibernate session which is needed when you do some write operations, but as we only need to perform the search, we can safely mention <code>readOnly<\/code> property to <code>true<\/code>.<\/p>\n<h2>9. Defining JPA Repository<\/h2>\n<p>As most of the operations are done by JPA Repository itself, let&#8217;s define it here:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>PersonRepository.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">@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 gracefully<\/li>\n<li>We used <code>Person<\/code> as a parameter to signify that this JPA interface will manage the Person 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 Long<\/li>\n<\/ul>\n<h2>10. Defining Data Access Layer (DAL) Interface<\/h2>\n<p>Although we have defined the JPA Repository which performs all the CRUD operations, we still will be making a DAL layer which defines the queries for Hibernate free-text search. Let us look at the contract we define:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>PersonDAL.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">public interface PersonDAL {\n    List&lt;Person&gt; fuzzySearchPerson(String term);\n    List&lt;Person&gt; wildCardSearchPerson(String term);\n}\n<\/pre>\n<h2>11. Implementing DAL Interface<\/h2>\n<p>In the DAL Interface we defined, we will be implementing two different type of free-text search:<\/p>\n<ul>\n<li>Fuzzy Search: Fuzzy search is applicable when we want to find terms which are at a distance from the search term. To understand the gap, let us consider an example. The term <code>Hibernate<\/code> and <code>Hibernat<\/code> has a gap of 1 due to missing <code>e<\/code> in later word, the terms <code>Hibernate<\/code> and <code>Hibernawe<\/code> also has a gap of 1 as a single character in later String i.e. <code>w<\/code> can be replaced to form the former String.<\/li>\n<li>Wildcard Search: These re just like SQL statements which have a matching phrase. Like matching phrases for <code>Hibernate<\/code> can be <code>Hiber<\/code>, <code>bernate<\/code> etc.<\/li>\n<\/ul>\n<p>Let us implement both of these in our DAL layer.<\/p>\n<h3>11.1 Defining Fuzzy Query<\/h3>\n<p>We will be starting with Fuzzy search implementation. This is a very intelligent and complex search as this needs Tokenization of each term saved in the database indexes. Read more about how Lucene do this <a href=\"https:\/\/www.javacodegeeks.com\/2015\/09\/introduction-to-lucene.html\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>Let us implement this search query here:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Fuzzy Query<\/em><\/span><\/p>\n<pre class=\"brush:java\">@PersistenceContext\nprivate EntityManager entityManager;\n\n@Override\npublic List&lt;Person&gt; fuzzySearchPerson(String term) {\n\n    FullTextEntityManager fullTextEntityManager =\n            org.hibernate.search.jpa.Search.\n                    getFullTextEntityManager(entityManager);\n\n    QueryBuilder queryBuilder =\n            fullTextEntityManager.getSearchFactory()\n                    .buildQueryBuilder().forEntity(Person.class).get();\n\n    Query fuzzyQuery = queryBuilder\n            .keyword()\n            .fuzzy()\n            .withEditDistanceUpTo(2)\n            .withPrefixLength(0)\n            .onField(\"name\")\n            .matching(term)\n            .createQuery();\n\n    FullTextQuery jpaQuery =\n            fullTextEntityManager.createFullTextQuery(fuzzyQuery, Person.class);\n\n    return jpaQuery.getResultList();\n}\n<\/pre>\n<p>We just used the edit distance of 2. This is the maximum gap which is supported by Hibernate and Lucene engine.<\/p>\n<h3>11.2 Defining Wildcard Query<\/h3>\n<p>Wildcard query is easy to understand and implement. This works just like SQL LIKE statements:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Wildcard Query<\/em><\/span><\/p>\n<pre class=\"brush:java\">    @Override\n    public List&lt;Person&gt; wildCardSearchPerson(String term) {\n        FullTextEntityManager fullTextEntityManager =\n                org.hibernate.search.jpa.Search.\n                        getFullTextEntityManager(entityManager);\n\n        QueryBuilder queryBuilder =\n                fullTextEntityManager.getSearchFactory()\n                        .buildQueryBuilder().forEntity(Person.class).get();\n\n        Query wildcardQuery = queryBuilder\n                .keyword()\n                .wildcard()\n                .onField(\"name\")\n                .matching(\"*\" + term + \"*\")\n                .createQuery();\n\n\n        FullTextQuery jpaQuery =\n                fullTextEntityManager.createFullTextQuery(wildcardQuery, Person.class);\n\n        return jpaQuery.getResultList();\n    }\n<\/pre>\n<p>We applied <code>*<\/code> to front and back of the term so that LIKE can work in both the directions.<\/p>\n<h2>12. Building search Index in Hibernate<\/h2>\n<p>Before Hibernate can start storing the index data, we need to make sure that the search index actually exists. This can be done by constructing it as soon as the application starts:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>BuildSearchIndex.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">@Component\npublic class BuildSearchIndex implements ApplicationListener&lt;ApplicationReadyEvent&gt; {\n\n    @PersistenceContext\n    private EntityManager entityManager;\n\n    @Override\n    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {\n        try {\n            FullTextEntityManager fullTextEntityManager =\n                    Search.getFullTextEntityManager(entityManager);\n            fullTextEntityManager.createIndexer().startAndWait();\n        }\n        catch (InterruptedException e) {\n            System.out.println(\n                    \"An error occurred trying to build the serach index: \" +\n                            e.toString());\n        }\n        return;\n    }\n}\n<\/pre>\n<p>Although we have constructed a search index in Hibernate but where will the indexed data be stored. We will configure this next.<\/p>\n<h2>13. Storing Index data<\/h2>\n<p>As Hibernate needs to store the Index data so that it doesn&#8217;t have to rebuild it every time an operation is performed, we will provide Hibernate with filesystem directory where it can store this data:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>application.properties<\/em><\/span><\/p>\n<pre class=\"brush:bash\"># Specify the Lucene Directory\nspring.jpa.properties.hibernate.search.default.directory_provider = filesystem\n\n# Using the filesystem DirectoryProvider you also have to specify the default\n# base directory for all indexes\nspring.jpa.properties.hibernate.search.default.indexBase = indexpath\n<\/pre>\n<p>The <code>directory_provider<\/code> just provides what type of system will store the data as we can even store indexing data to the cloud.<\/p>\n<h2>14. Creating a Command-Line Runner<\/h2>\n<p>Now, we&#8217;re ready to run our project. To insert sample data into<\/p>\n<p><span style=\"text-decoration: underline;\"><em>DataJpaApp.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">@SpringBootApplication\npublic class DataJpaApp implements CommandLineRunner {\n\n\tprivate static final Logger LOG = LoggerFactory.getLogger(\"JCG\");\n\n\t@Autowired\n\tprivate PersonService service;\n\n\tpublic static void main(String[] args) {\n\t\tSpringApplication.run(DataJpaApp.class, args);\n\t}\n\n\t@Override\n\tpublic void run(String... strings) {\n\n\t\tLOG.info(\"Current objects in DB: {}\", service.countPersons());\n\n\t\tPerson person = service.createPerson(new Person(\"Shubham\", 23));\n\t\tLOG.info(\"Person created in DB : {}\", person);\n\n\t\tLOG.info(\"Current objects in DB: {}\", service.countPersons());\n\n\t\tList&lt;Person&gt; fuzzySearchedPersons = service.fuzzySearchPerson(\"Shubha\");\n\t\tLOG.info(\"Founds objects in fuzzy search: {}\", fuzzySearchedPersons.get(0));\n\n\t\tList&lt;Person&gt; wildSearchedPersons = service.wildCardSearchPerson(\"hub\");\n\t\tLOG.info(\"Founds objects in wildcard search: {}\", wildSearchedPersons.get(0));\n\n\t\tperson.setName(\"Programmer\");\n\t\tPerson editedPerson = service.editPerson(person);\n\t\tLOG.info(\"Person edited in DB  : {}\", person);\n\n\t\tservice.deletePerson(person);\n\t\tLOG.info(\"After deletion, count: {}\", service.countPersons());\n\t}\n}\n<\/pre>\n<h2>15. Running the Project with Maven<\/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_76349\" aria-describedby=\"caption-attachment-76349\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/run-hibernate-project.png\"><img decoding=\"async\" class=\"size-full wp-image-76349\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/run-hibernate-project.png\" alt=\"Run Hibernate Project\" width=\"820\" height=\"155\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/run-hibernate-project.png 820w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/run-hibernate-project-300x57.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/run-hibernate-project-768x145.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-76349\" class=\"wp-caption-text\">Run Hibernate Project<\/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, called the search methods to obtain expected results.<\/p>\n<h2>16. Conclusion<\/h2>\n<p>In this lesson, we studied how we can use Hibernate to configure Spring Data APIs and how it can help us to automatically construct Tables in our database just by defining POJO classes for our entities. Even when we update our entities, we need not to worry about making changes in the database!<\/p>\n<p>We also ran examples with Hibernate search which our powered by Lucene engine itself for indexing and Hibernate provides us useful wrapper over Lucene functionalities.<\/p>\n<h2>17. Download the Source Code<\/h2>\n<p>This was an example of with Spring Boot and Hibernate ORM Framework.<\/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\/04\/JCG-BootHibernate-Example.zip\" target=\"_blank\" rel=\"noopener\">JCG-BootHibernate-Example<\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction In this post, we shall demonstrate how to leverage the power of one of the most popular ORM (object-relational mapping) tool, Hibernate which facilitates the conversion of an object-oriented domain model to a traditional relational database. Hibernate is one of the most popular Java frameworks out there. For this reason we have provided &hellip;<\/p>\n","protected":false},"author":20016,"featured_media":240,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[971,30,854],"class_list":["post-76169","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-hibernate","tag-spring","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 Hibernate Tutorial - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"1. Introduction In this post, we shall demonstrate how to leverage the power of one of the most popular ORM (object-relational mapping) tool, Hibernate\" \/>\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\/04\/spring-hibernate-tutorial.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Hibernate Tutorial - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"1. Introduction In this post, we shall demonstrate how to leverage the power of one of the most popular ORM (object-relational mapping) tool, Hibernate\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-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-04-28T12:00:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-11T10:13:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-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=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-tutorial.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-tutorial.html\"},\"author\":{\"name\":\"Shubham Aggarwal\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/0953481a8babbb7a63907edb41f357ad\"},\"headline\":\"Spring Hibernate Tutorial\",\"datePublished\":\"2018-04-28T12:00:46+00:00\",\"dateModified\":\"2023-12-11T10:13:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-tutorial.html\"},\"wordCount\":1711,\"commentCount\":11,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"keywords\":[\"Hibernate\",\"Spring\",\"Spring Boot\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-tutorial.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-tutorial.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-tutorial.html\",\"name\":\"Spring Hibernate Tutorial - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-tutorial.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"datePublished\":\"2018-04-28T12:00:46+00:00\",\"dateModified\":\"2023-12-11T10:13:57+00:00\",\"description\":\"1. Introduction In this post, we shall demonstrate how to leverage the power of one of the most popular ORM (object-relational mapping) tool, Hibernate\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-tutorial.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-tutorial.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-tutorial.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"spring-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/04\\\/spring-hibernate-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 Hibernate 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\\\/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 Hibernate Tutorial - Java Code Geeks","description":"1. Introduction In this post, we shall demonstrate how to leverage the power of one of the most popular ORM (object-relational mapping) tool, Hibernate","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\/04\/spring-hibernate-tutorial.html","og_locale":"en_US","og_type":"article","og_title":"Spring Hibernate Tutorial - Java Code Geeks","og_description":"1. Introduction In this post, we shall demonstrate how to leverage the power of one of the most popular ORM (object-relational mapping) tool, Hibernate","og_url":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2018-04-28T12:00:46+00:00","article_modified_time":"2023-12-11T10:13:57+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-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":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html"},"author":{"name":"Shubham Aggarwal","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/0953481a8babbb7a63907edb41f357ad"},"headline":"Spring Hibernate Tutorial","datePublished":"2018-04-28T12:00:46+00:00","dateModified":"2023-12-11T10:13:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html"},"wordCount":1711,"commentCount":11,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","keywords":["Hibernate","Spring","Spring Boot"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html","url":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html","name":"Spring Hibernate Tutorial - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","datePublished":"2018-04-28T12:00:46+00:00","dateModified":"2023-12-11T10:13:57+00:00","description":"1. Introduction In this post, we shall demonstrate how to leverage the power of one of the most popular ORM (object-relational mapping) tool, Hibernate","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-tutorial.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","width":150,"height":150,"caption":"spring-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2018\/04\/spring-hibernate-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 Hibernate 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\/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\/76169","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=76169"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/76169\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/240"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=76169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=76169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=76169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}