How do I create JPA EntityManagerFactory?

In this code snippet you will learn how to create JPA EntityManagerFactory. This factory enable you to create the EntityManager which will be used to execute the JPA command to manipulate the database tables.

To create the EntityManagerFactory you need to create to persistence.xml file first. The file is where you configure the JPA. This file must be placed inside the META-INF directory in your program working directory.

Here is an example of the persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="music" transaction-type="RESOURCE_LOCAL">
        <class>org.kodejava.jpa.entity.Artist</class>
        <class>org.kodejava.jpa.entity.Genre</class>
        <class>org.kodejava.jpa.entity.Label</class>
        <class>org.kodejava.jpa.entity.Record</class>
        <class>org.kodejava.jpa.entity.Review</class>
        <class>org.kodejava.jpa.entity.Reviewer</class>
        <class>org.kodejava.jpa.entity.Track</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/musicdb" />
            <property name="javax.persistence.jdbc.user" value="music" />
            <property name="javax.persistence.jdbc.password" value="s3cr*t" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>

</persistence>

The persistence unit defined in the persistence.xml file contains a set of entities object. We also define some properties related to the database connections including the JDBC driver class, JDBC url, the username and password for opening the connection to database.

After defining the persistence.xml file we’ll create a simple program to create the EntityManagerFactory. To create the factory we can use the javax.persistence.Persistence class createEntityManagerFactory() method and pass the persistence unit name as the parameter. In this example the persistence unit name is music as can be seen in the persistence.xml file.

After we have the factory object created we can then create an EntityManager by calling the createEntityManager() of the factory object. Let’s see the code snippet below.

package org.kodejava.jpa;

import org.kodejava.jpa.entity.Artist;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class EntityManagerFactoryExample {
    public static final String PERSISTENCE_UNIT_NAME = "music";

    public static void main(String[] args) {
        EntityManagerFactory factory =
                Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        EntityManager manager = factory.createEntityManager();

        // Do something with the entity manager.
        Artist artist = manager.find(Artist.class, 1L);
        System.out.println("artist = " + artist);
    }
}

The Artist entity class definition.

package org.kodejava.jpa.entity;

import javax.persistence.*;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;

@Entity
@Table(name = "artist")
public class Artist implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    private Long id;
    private String name;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "name", length = 100, nullable = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Artist artist = (Artist) o;
        return Objects.equals(id, artist.id) &&
                Objects.equals(name, artist.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }

    @Override
    public String toString() {
        return "Artist{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

Our project directory structure.

├─ pom.xml
└─ src
   └─ main
      ├─ java
      │  └─ org
      │     └─ kodejava
      │        └─ jpa
      │           ├─ EntityManagerFactoryExample.java
      │           └─ entity
      │              ├─ Artist.java
      │              ├─ Genre.java
      │              ├─ Label.java
      │              ├─ Record.java
      │              ├─ Review.java
      │              ├─ Reviewer.java
      │              └─ Track.java
      └─ resources
         └─ META-INF
            └─ persistence.xml

Maven Dependencies

<dependencies>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.6.9.Final</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.1.0</version>
    </dependency>
</dependencies>

Maven Central Maven Central Maven Central

How do I create entity object in JPA?

This example show you a simple example of an entity object used for mapping database table into Java object. The entity is a Plain Old Java Object (POJO). The JPA specification doesn’t mandate the class to extends or implements other class or interfaces.

A class which going to be persisted in a database must be annotated with the javax.persistence.Entity annotation (@Entity). As you can see in the Record class below.

By default, the mapped table name equals to the class name. But if your table name is different to your class name you can use the @Table annotation. Set the table name using the name attribute of this annotation. This annotation is also located in the javax.persistence package.

package org.kodejava.jpa.entity;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "record")
public class Record implements Serializable {
}

In JPA metadata can be added either in the class fields or using the getters or setters methods. Choose one option because you cannot mix both of them in the same entity object. Here we will annotate the getters of the class.

To define the primary key of the entity we use the @Id annotation. The @GeneratedValue annotation is used to define how the primary key of the entity should be generated. For example in this example the strategy is defined as GenerationType.IDENTITY. In MySQL database this is implemented as an auto-increment column.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

The fields of the entity by default will be persisted into corresponding fields in the database table. If you don’t want the entity fields to be persisted you must add the @Transient annotation to it. If your entity field name is different with table field you can use the @Column annotation to define the column name and other attributes of the column such as the length, the uniqueness of the field and the not-null attribute.

To define relationship between entity object you can use annotation such as @OneToOne, @OneToMany, @ManyToOne and @ManyToMany. This annotation represent the relationship between database tables in the Java objects.

@Column(nullable = false, length = 50)
public String getTitle() {
    return title;
}

@Column(name = "release_date")
public Date getReleaseDate() {
    return releaseDate;
}

@ManyToOne
@JoinColumn(nullable = false)
public Artist getArtist() {
    return artist;
}

Below is the complete class for the Record entity. This will hold information about music record. This entity has relationship with other entity such the Artist and Label entity.

package org.kodejava.jpa.entity;

import javax.persistence.*;
import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Entity
@Table(name = "record")
public class Record implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    private Long id;
    private String title;
    private Date releaseDate;
    private Artist artist;
    private Label label;

    private List<Track> trackList = new ArrayList<>();

    public Record() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    @Column(nullable = false, length = 50)
    public String getTitle() {
        return title;
    }

    @Column(name = "release_date")
    public Date getReleaseDate() {
        return releaseDate;
    }

    @ManyToOne
    @JoinColumn(nullable = false)
    public Artist getArtist() {
        return artist;
    }

    @ManyToOne
    @JoinColumn(nullable = false)
    public Label getLabel() {
        return label;
    }

    @OneToMany(mappedBy = "record")
    public List<Track> getTrackList() {
        return trackList;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setReleaseDate(Date releaseDate) {
        this.releaseDate = releaseDate;
    }

    public void setArtist(Artist artist) {
        this.artist = artist;
    }

    public void setLabel(Label label) {
        this.label = label;
    }

    public void setTrackList(List<Track> trackList) {
        this.trackList = trackList;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Record record = (Record) o;
        return Objects.equals(id, record.id) &&
                Objects.equals(title, record.title) &&
                Objects.equals(releaseDate, record.releaseDate);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, title, releaseDate);
    }

    @Override
    public String toString() {
        return "Record{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", releaseDate=" + releaseDate +
                '}';
    }
}

Maven Dependencies

<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>

Maven Central

What is JPA (Java Persistence API)?

JPA is a Java specification for object-relational mapping (ORM) in Java. JPA provide a way to map Java objects to database tables. This allows programmers to manipulate database information directly using Java objects instead of executing database SQL queries.

Developer can choose one of many available JPA specification implementation libraries such as Hibernate, Apache OpenJPA and EclipseLink. EclipseLink is the reference implementation of the JPA specification. In the examples that we are going to provide you in this website, Hibernate library will be used as the persistence provider.

In JPA we model our database tables into a Java objects. This Java objects also called as entity objects. The entity represent a table in database. A single row in a database table will be represented in an instance of the entity. This entity objects hold information about the mapping between objects and database tables. This information or metadata can be defined using an annotation or an XML mapping files.

Here is a simple example of entity object and its metadata information.

package org.kodejava.jpa.entity;

import javax.persistence.*;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;

@Entity
@Table(name = "genre")
public class Genre implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    private Long id;
    private String name;

    public Genre() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

    @Column(nullable = false, length = 50)
    public String getName() {
        return name;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Genre genre = (Genre) o;
        return Objects.equals(id, genre.id) &&
                Objects.equals(name, genre.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }

    @Override
    public String toString() {
        return "Genre{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

In the entity above we use annotation such as @Entity, @Table, @Id, @GeneratedValue and @Column. These are some annotations that you can use for object mapping.

Beside manipulating database tables using objects, JPA also provide a SQL-like queries that can be used to create a static or dynamic query statement.

Maven Dependencies

<dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>javax.persistence-api</artifactId>
    <version>2.2</version>
</dependency>

Maven Central

How do I do math operations using Spring EL?

As we know that the simplest value that can be expressed in the Spring EL is a literal value such as number. Furthermore, we can also do math operations using the Spring EL. The spring configuration below show you how can do math operations using the Spring Expression Language. These operations include:

  • Add operation (+)
  • Subtract operation (-)
  • Multiply operation (*)
  • Divide operations (/)
  • Modulo operation (%)
  • Power operation (^)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="myBean" class="org.kodejava.spring.core.el.MyBean">
        <property name="total" value="#{50 + 50}" />
        <property name="length" value="#{100 - 10}" />
        <property name="size" value="#{10 * 10}" />
        <property name="reminder" value="#{10 % 3}" />
        <property name="distance" value="#{1000 / 10}" />
        <property name="power" value="#{2 ^ 10}" />
    </bean>

</beans>

The configuration above requires a bean / pojo called MyBean. It’s a simple class with some fields and getters and setters. Followed later by a simple class called SpELMathOperationDemo that demonstrate the Spring EL math operations.

package org.kodejava.spring.core.el;

public class MyBean {
    private int total;
    private int length;
    private int size;
    private float distance;
    private int reminder;
    private int power;

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public float getDistance() {
        return distance;
    }

    public void setDistance(float distance) {
        this.distance = distance;
    }

    public int getReminder() {
        return reminder;
    }

    public void setReminder(int reminder) {
        this.reminder = reminder;
    }

    public int getPower() {
        return power;
    }

    public void setPower(int power) {
        this.power = power;
    }
}
package org.kodejava.spring.core.el;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpELMathOperationDemo {
    public static void main(String[] args) {
        try (ClassPathXmlApplicationContext context =
                     new ClassPathXmlApplicationContext("spel-math-operation.xml")) {
            MyBean bean = (MyBean) context.getBean("myBean");
            System.out.println("bean.getTotal()    = " + bean.getTotal());
            System.out.println("bean.getLength()   = " + bean.getLength());
            System.out.println("bean.getSize()     = " + bean.getSize());
            System.out.println("bean.getReminder() = " + bean.getReminder());
            System.out.println("bean.getDistance() = " + bean.getDistance());
            System.out.println("bean.getPower()    = " + bean.getPower());
        }
    }
}

And these are the output printed out by the code snippet:

bean.getTotal()    = 100
bean.getLength()   = 90
bean.getSize()     = 100
bean.getReminder() = 1
bean.getDistance() = 100.0
bean.getPower()    = 1024

Maven Dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.23</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.3.23</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>5.3.23</version>
    </dependency>
</dependencies>

Maven Central Maven Central Maven Central

How do I call static method using Spring EL?

In this example you will learn how to call static method using Spring EL. The T() operator of the Spring EL can be used to call static method. First, create the following class, NumberGenerator. This class has a single property randomNumber and getter / setter method.

package org.kodejava.spring.core.el;

public class NumberGenerator {
    private int randomNumber;

    public int getRandomNumber() {
        return randomNumber;
    }

    public void setRandomNumber(int randomNumber) {
        this.randomNumber = randomNumber;
    }
}

Now, create the following spring configuration file and save it in a file called SpELStatic.xml. In this configuration we register a bean called bean of type NumberGenerator. We set its randomNumber property using the value produced by the java.lang.Math.random() static method. For calling a static method we use the Spring EL T() operator, for example #{T(java.lang.Math).random()}.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="bean" class="org.kodejava.spring.core.el.NumberGenerator">
        <property name="randomNumber" value="#{T(java.lang.Math).random() * 100 + 1}" />
    </bean>

</beans>

The program below load the spring configuration and get the NumberGenerator bean to create a random number.

package org.kodejava.spring.core.el;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StaticELDemo {
    public static void main(String[] args) {
        try (ClassPathXmlApplicationContext context =
                     new ClassPathXmlApplicationContext("spel-static.xml")) {

            NumberGenerator number = (NumberGenerator) context.getBean("bean");
            System.out.println("Random number: " + number.getRandomNumber());
        }
    }
}

And example result you might get when running the program:

Random number: 33

Maven Dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.23</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.3.23</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>5.3.23</version>
    </dependency>
</dependencies>

Maven Central Maven Central Maven Central