= JPA Hibernate Arquillian
:index-group: JPA
:jbake-type: page
:jbake-status: published

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.

== Movie

Here we have a class with some details. See the annotation 
link:https://tomee.apache.org/tomee-9.0/javadoc/jakarta/persistence/Entity.html[@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 
link:https://tomee.apache.org/tomee-9.0/javadoc/jakarta/persistence/Id.html[@Id]
annotation, it indicates that this attribute is the identifier of the entity and the other annotation 
link:https://tomee.apache.org/tomee-9.0/javadoc/jakarta/persistence/GeneratedValue.html[@GeneratedValue]
indicates that the unique identifier value generation of the entity will be managed by the persistence provider.

[source,java]
----
package org.superbiz.injection.h5jpa;

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

@Entity
public class Movie {

    @Id
    @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;
    }

}
----

== Movies

Now we have two important things: 
link:https://tomee.apache.org/tomee-9.0/javadoc/jakarta/persistence/PersistenceContext.html[@PersistenceContext]
annotation and the 
link:https://tomee.apache.org/tomee-9.0/javadoc/jakarta/persistence/EntityManager.html[EntityManager]
declaration.
The 
link:https://tomee.apache.org/tomee-9.0/javadoc/javax/persistence/EntityManager.html[EntityManager]
is the interface with the core methods of JPA like persist, remove, merge, find and others...
We annotate the 
link:https://tomee.apache.org/tomee-9.0/javadoc/jakarta/persistence/EntityManager.html[EntityManager]
with 
link:https://tomee.apache.org/tomee-9.0/javadoc/jakarta/persistence/PersistenceContext.html[@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.

[source,java]
----
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;

@Stateful
public class Movies {

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

    public void addMovie(Movie movie) throws Exception {
        entityManager.persist(movie);
    }

    public void deleteMovie(Movie movie) throws Exception {
        entityManager.remove(movie);
    }

    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);
        cq.select(qb.count(root));
        if (field != null && searchTerm != null && !"".equals(field.trim()) && !"".equals(searchTerm.trim())) {
            Path<String> path = root.get(type.getDeclaredSingularAttribute(field.trim(), String.class));
            Predicate condition = qb.like(path, "%" + searchTerm.trim() + "%");
            cq.where(condition);
        }
        return entityManager.createQuery(cq).getSingleResult().intValue();
    }

}
----

== persistence.xml

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="http://java.sun.com/xml/ns/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">
    <persistence-unit name="movie-unit">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>movieDatabase</jta-data-source>
        <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
        <class>org.superbiz.injection.h3jpa.Movie</class>
        <properties>
            <property name="hibernate.hbm2ddl.auto" 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"/>
        </properties>
    </persistence-unit>
</persistence>
----

You always can refer to https://tomee.apache.org/latest/docs/datasource-config.html[Datasource configuration] documentation for more details.

== arquillian.xml
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 https://tomee.apache.org/latest/docs/tomee-and-hibernate.html[TomEE and Hibernate] documentation.


----
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
    <container qualifier="tomee" default="true">
        <configuration>
            <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-->
                mvn:org.hibernate:hibernate-core-jakarta:5.6.8.Final
                mvn:org.hibernate.common:hibernate-commons-annotations:5.1.2.Final
                mvn:antlr:antlr:2.7.7
                mvn:org.jboss:jandex:2.4.2.Final
                mvn:org.jboss.logging:jboss-logging:3.3.2.Final
                mvn:net.bytebuddy:byte-buddy:1.12.8
                mvn:com.fasterxml:classmate:1.5.1
            </property>
        </configuration>
    </container>
</arquillian>
----




== MoviesArquillianTest

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 database.
- Insert a second movie.
- Delete the first movie
- Confirm that the second movie is the only available in the database.

[source,java]
----
package org.superbiz.injection.h5jpa;

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;

@RunWith(Arquillian.class)
public class MoviesArquillianTest {

    @Deployment
    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");
    }

    @EJB
    private Movies movies;

    @Test
    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");
        movie.setYear(1995);
        movies.addMovie(movie);

        //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());

        //Delete
        movies.deleteMovie(movie);

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

= Running

To run the example via maven:

Access the project folder:
[source,java]
----
cd jpa-hibernate-arquillian
----
And execute:
[source,java]
----
mvn clean install
----

Which will generate output similar to the following:

[source,console]
----
...
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] org.hibernate.annotations.common.reflection.java.JavaReflectionManager.<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(movie0_.id) 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 movie0_.id 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 movie0_.id 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
----
