Brief notes on Java EE’s JPA and its implementation, mainly by Hibernate

Last Updated on: (senast uppdaterad på:)

Database interactions in a Java EE application are done through JPA.

  • JPA is a specification that defines the management of relational data in a Java application.
  • JPA is a specification and needs an implementation
  • JPA has concepts that an implementation must cover.
    • Entity
      through the javax.persistence.Entity class
    • Field Persistence
      All fields in the object are persisted unless annotated with @Transient.
    • Relationships
      JPA specifies how we should manage relationships between different database tables. @OneToOne, @OneToMany, @ManyToOne, @ManyToMany
    • EntityManager
      through the javax.persistence.EntityManager class —
      EntityManager contains common Create, Read, Update and Delete (CRUD) operations that are “persisted” to the database.

JPA has implementations available such as Hibernate and EclipseLink. Otherwise there is a lot of code to write classes for the above. Benchmarks for the implementations are available.
Hibernate is one of the mature implementations (described below). EclipseLink is another implementation, which is open source,
supports a number of other persistence standards such as Java Architecture for XML Binding (JAXB) and has an XML Representation. EclipseLink has extensions of its own such as @ReadOnly, @Struct while not having an annotation such as @BatchSize which is featured in Hibernate.

Hibernate notes (part 1)

This useful information was found at https://www.baeldung.com/jpa-hibernate-difference
  • with @Entity annotation it uses:
    @org.hibernate.annotations.Entity
  • has also annotations that extend the functionality of @Entity:
    @Table — specify name of table
    @BatchSize — specify batch size when retrieving entities
  • has few extra features that JPA does not specify that may prove useful in larger applications:
    1. Customizable CRUD statements with @SQLInsert, @SQLUpdate, @SQLDelete annotations.
    2. Support for soft deleting
    3. Immutable entities with @Immutable annotation

Hibernate notes (part 2)

This useful information, specifically the examples page can be found at https://www.tutorialspoint.com/hibernate/hibernate_examples.html

A series offered at tutorialpoint.com/hibernate provides Hibernate learning material. The series start with a Home and comprises readings on:

  • ORM Overview
  • Hibernate Overview
  • Architecture
  • Environment
  • Configuration
  • Sessions
  • Persistent Class
  • Mapping Files
  • Mapping Types
  • Hibernate Examples
  • O/R Mappings
  • Annotations
  • Query Language
  • Criteria Queries
  • Native SQL
  • Caching
  • Batch Processing
  • Interceptors
  • ORM

Hibernate notes (part 3)

The useful tutorial is found at: http://www.javawebtutor.com/articles/jpa/jpa-example-using-maven.php

Steps for a Maven JPA application with its persistence dependencies are as below.

  1. Create the POJO entities classes
  2. Add the dependencies to the pom.xml file.
    Artifact IDs are:
    1. javax.persistence
    2. hibernate-entitymanager
    3. mysql-connector-java
  3. set up persistence.xml file in META-INF folder
  4. run a test class

Notes about POJO classes while developing with JPA

  • the class should implement java.io.Serializable
  • POJO entity classes that have fields with one-to-many relationships should be annotated by the annotations that show the relationship. Some of these annotations are @OneToMany and @ManyToOne, @JoinColumn, @mappedBy. A bidirectional relationship between two tables would have annotations @OneToMany and @ManyToOne.
  • if the primary key “id” of the entity class does not have the annotation @GeneratedValue(strategy=GenerationType.AUTO) then one can run the EntityManager’s persist with the entity class that assigns id manually. However, if the primary key “id” has the annotation @GeneratedValue(strategy=GenerationType.AUTO) then entity manager’s persist can be used with a persist that does not have the id included.

Other resources through out the development (in order of occurrence):

Lämna ett svar