Preloader image

This example shows the persist, remove and creation of an entity with JPA and Hibernate 5 using arquillian for the test.

The Java Persistence API (JPA) is a Java specification for accessing, persisting, and managing data between Java objects / classes and a relational database.

To exemplify the use of JPA, we will persist an Object (Movie) in the database.

Links to the documentation have been added in key parts of the example for the case of doubts and as a way to encourage their reading for details.


Here we have a class with some details. See the annotation @Entity above the declaration, with it we are saying that this class is an entity (a table in the database). We still have two more annotations above the attribute id, one of them is @Id annotation, it indicates that this attribute is the identifier of the entity and the other annotation @GeneratedValue indicates that the unique identifier value generation of the entity will be managed by the persistence provider.

package org.superbiz.injection.h3jpa;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

public class Movie {

    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String director;
    private String title;
    private int year;

    public Movie() {

    public Movie(String director, String title, int year) {
        this.director = director;
        this.title = title;
        this.year = year;

    public String getDirector() {
        return director;

    public void setDirector(String director) {
        this.director = director;

    public String getTitle() {
        return title;

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

    public int getYear() {
        return year;

    public void setYear(int year) {
        this.year = year;



Now we have two important things: @PersistenceContext annotation and the EntityManager declaration. The EntityManager is the interface with the core methods of JPA like persist, remove, merge, find and others…​ We annotate the EntityManager with @PersistenceContext, a persistence context is an entity management where every persistence context associated with a persistence unit, we will create a persistence.xml soon for this.

import jakarta.ejb.Stateful;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.PersistenceContextType;
import jakarta.persistence.Query;
import jakarta.persistence.criteria.*;
import jakarta.persistence.metamodel.EntityType;
import java.util.List;

public class Movies {

    @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.EXTENDED)
    private EntityManager entityManager;

    public void addMovie(Movie movie) throws Exception {

    public void deleteMovie(Movie movie) throws Exception {

    public List<Movie> getMovies() throws Exception {
        Query query = entityManager.createQuery("SELECT m from Movie as m");
        return query.getResultList();

    public int count(String field, String searchTerm) {
        CriteriaBuilder qb = entityManager.getCriteriaBuilder();
        CriteriaQuery<Long> cq = qb.createQuery(Long.class);
        Root<Movie> root = cq.from(Movie.class);
        EntityType<Movie> type = entityManager.getMetamodel().entity(Movie.class);;
        if (field != null && searchTerm != null && !"".equals(field.trim()) && !"".equals(searchTerm.trim())) {
            Path<String> path = root.get(type.getDeclaredSingularAttribute(field.trim(), String.class));
            Predicate condition =, "%" + searchTerm.trim() + "%");
        return entityManager.createQuery(cq).getSingleResult().intValue();



Here we define which database will persist our movies, and we perform other configurations such as: define a persistence-unit with the name movie-unit, followed by the definition of the JPA provider/implementation (in this case Hibernate 5) and we set some properties for hibernate 5:

 <persistence xmlns=""
    <persistence-unit name="movie-unit">
            <property name="" value="update"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.show_sql" value="true"/>

            JPA and CDI are linked, enabling JPA to use CDI for its
            components but CDI can use JPA too. To solve issues with
            hibernate you need to this property either as system property
            or persistence unit
            <property name="tomee.jpa.factory.lazy" value="true"/>

You always can refer to Datasource configuration documentation for more details.


This file provides the configuration the server will have for running the tests. The property additionalLibs provide to the server the jar files required for Hibernate 5 as explained in the TomEE and Hibernate documentation.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian xmlns=""
    <container qualifier="tomee" default="true">
            <property name="httpPort">-1</property>
            <property name="ajpPort">-1</property>
            <property name="stopPort">-1</property>
            <property name="dir">target/tomee-remote</property>
            <property name="appWorkingDir">target/arquillian-remote-working-dir</property>
            <property name="cleanOnStartUp">true</property>
            <property name="additionalLibs">
                <!-- add hibernate 5 need it jars to the server-->



Now we do a test with the following workflow:

  • Insert a movie.

  • Confirm that a movie was persisted by querying the number of movies from the data base.

  • Insert a second movie.

  • Delete the first movie

  • Confirm that the second movie is the only available in the data base.

package org.superbiz.injection.h3jpa;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import jakarta.ejb.EJB;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class MoviesArquillianTest {

    public static WebArchive createDeployment() {
        return ShrinkWrap.create(WebArchive.class, "test.war")
                .addClasses(Movie.class, Movies.class, MoviesArquillianTest.class)
                .addAsResource(new ClassLoaderAsset("META-INF/persistence.xml"), "META-INF/persistence.xml");

    private Movies movies;

    public void shouldBeAbleToAddAMovie() throws Exception {
        assertNotNull("Verify that the ejb was injected", movies);

        //Insert a movie
        final Movie movie = new Movie();
        movie.setDirector("Michael Bay");
        movie.setTitle("Bad Boys");

        //Count movies
        assertEquals(1, movies.count("title", "a"));

        //Insert a movie
        movies.addMovie(new Movie("David Dobkin", "Wedding Crashers", 2005));

        //Get movies
        assertEquals(2, movies.getMovies().size());


        //Get movies
        assertEquals(2005, movies.getMovies().get(0).getYear());


To run the example via maven:

Access the project folder:

cd jpa-hibernate-arquillian

And execute:

mvn clean install

Which will generate output similar to the following:

INFO [http-nio-56012-exec-3] org.apache.myfaces.webapp.StartupServletContextListener.contextInitialized MyFaces Core has started, it took [2112] ms.
INFO [http-nio-56012-exec-5] org.hibernate.jpa.internal.util.LogHelper.logPersistenceUnitInformation HHH000204: Processing PersistenceUnitInfo [name: movie-unit]
INFO [http-nio-56012-exec-5] org.hibernate.Version.logVersion HHH000412: Hibernate Core {5.4.10.Final}
INFO [http-nio-56012-exec-5]<clinit> HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
INFO [http-nio-56012-exec-5] org.hibernate.dialect.Dialect.<init> HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect
INFO [http-nio-56012-exec-5] org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator.initiateService HHH000490: Using JtaPlatform implementation: [org.apache.openejb.hibernate.OpenEJBJtaPlatform2]
Hibernate: create table Movie (id bigint not null, director varchar(255), title varchar(255), year integer not null, primary key (id))
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
INFO [http-nio-56012-exec-5] org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory.createDelegate PersistenceUnit(name=movie-unit, provider=org.hibernate.jpa.HibernatePersistenceProvider) - provider time 4033ms
Hibernate: call next value for hibernate_sequence
Hibernate: insert into Movie (director, title, year, id) values (?, ?, ?, ?)
Hibernate: select count( as col_0_0_ from Movie movie0_ where movie0_.title like ?
Hibernate: call next value for hibernate_sequence
Hibernate: insert into Movie (director, title, year, id) values (?, ?, ?, ?)
Hibernate: select as id1_0_, movie0_.director as director2_0_, movie0_.title as title3_0_, movie0_.year as year4_0_ from Movie movie0_
Hibernate: delete from Movie where id=?
Hibernate: select as id1_0_, movie0_.director as director2_0_, movie0_.title as title3_0_, movie0_.year as year4_0_ from Movie movie0_
INFO [main] org.apache.openejb.assembler.classic.Assembler.doResourceDestruction Closing DataSource: Default Unmanaged JDBC Database
INFO [main] sun.reflect.DelegatingMethodAccessorImpl.invoke Destroying ProtocolHandler ["http-nio-56012"]
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0