Skip to content

Latest commit

 

History

History
97 lines (72 loc) · 4.57 KB

06_PersistenceWithJPA.adoc

File metadata and controls

97 lines (72 loc) · 4.57 KB

Persistence with Spring with Hibernate and JPA

Understanding how to use Hibernate and Spring with Java Persistence API.

Spring persistence with Hibernate JPA - Configuration

This example demonstrates persistence with Hibernate.

  1. Hibernate dependencies
    pom.xml
    Look for spring-jdbc, spring-orm, hibernate-core and hibernate-entitymanager dependencies.

  2. Application
    ColoredShapeApplication.java
    The class loads a Spring context through an AnnotationConfigApplicationContext.

  3. Config - the class that specifies the component scanning instruction. ⇒ ColoredShapeConfig.java
    The config class is supplied. This config class specifies the packages to be scanned for components. ⇒ HibernateJPAConfig.java
    The class that configures the HibernateEntityManager to connect to a database.

  4. Controller layer
    ColoredShapeControllingBean.java
    The class auto-wires a ColoredShapeService interface, the Spring context has a ColoredShapeServiceImpl that will be used as an auto-wired instance (remember the Liskov Substitution Principle). The controller invokes the service methods to persist and retrieve ColoredShape instances. The class is marked with a @Component annotation.

  5. Service layer
    ColoredShapeServiceImpl.java
    The class is marked with a @Service annotation. The @Service is an alias for the @Component`annotation (see the code). It should display a `@AliasFor(annotation = Component.class). The service implementation auto-wires a ColoredShapeDao interface and will access the ColoredShapeDaoImpl via the spring context. Methods in the service delegate to methods in the DAO.

  6. DAO layer
    ColoredShapeDaoImpl.java
    The class is marked with a @Repository annotation. The @Repository annotation is an alias for the @Component annotation. It too should have a @AliasFor(annotation = Component.class). The DAO implementation uses an instance of the SessionFactory to communicate with the database. Similar to the service, the DAO interface is wired with an implementation via the Spring context.

A new dependency hibernate-entitymanager is added. A Java configuration is used to setup the embedded database (XML could have been used as well).

Hibernate itself, is configured with external properties via a @PropertySource, pointing to an application.properties.

In addition to the embedded database and Hibernate configuration, a LocalContainerEntityManagerFactoryBean is configured to manage persistence for Hibernate.

The service layer invokes the DAO layer. The DAO layer declares an EntityManager, as a @PersistenceContext.

The transaction management is performed via the annotation @Transactional. The DAO is declared as read-only as default for the entire class and as read-write with propagation for the save method.

Quiz time

Quiz

  • What is Hibernate’s Spring JPA class for persistence?

  • What is the core Hibernate class for persistence?

  • Why is one preferred over the other?


Prev TOC Next

Persistence with Hibernate

TOC

Persistence with Spring Data JPA