Skip to content

FlorinAlexandru/javaee-persistence

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Create Java EE project with persistence

Steps

In this tutorial I will try to show you the basic steps to create an Java EE web application having persistence. We will use an in memory database (H2 in our tutorial) and we will populate it with some data.

  1. Create the Maven project

We will use an Maven archetype to create the base structure for our project.

mvn "-DarchetypeGroupId=org.codehaus.mojo.archetypes"
    "-DarchetypeArtifactId=webapp-javaee7"
    "-DarchetypeVersion=1.1"
    "-DgroupId=org.happypanda.persistence"
    "-DartifactId=persistence"
    "-Dversion=1.1.1"
    archetype:generate
  1. Add arquillian-bom dependency in dependencyManagement

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>1.1.15.Final</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
  1. Add junit, arquillian-junit-container, wildfly managed container

    <dependency>
        <groupId>org.wildfly</groupId>
        <artifactId>wildfly-arquillian-container-managed</artifactId>
        <version>8.2.0.Final</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>sun.jdk</groupId>
                <artifactId>jconsole</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
  1. Add persistence dependency

    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
        <scope>provided</scope>
    </dependency>
  1. Add persistence.xml file unde /resources/META-INF

The persistence.xml is where one configures all the properties for the persistence layer. It defines one or more persistence unit. A persistence unit defines a set of all entity classes that are managed by EntityManager instances in an application.

For use in testing, the persistence.xml file will be placed under src/test/resources. For production purpose, it will be placed unde src/main/resources/META-INF.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
             version="2.2">
    <persistence-unit name="inmemory" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="connection.driver_class" value="org.h2.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:h2:file:testdb"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="hibernate.show_sql" value="true" />
            <property name="javax.persistence.sql-load-script-source" value="insert.sql"/>
        </properties>
    </persistence-unit>
</persistence>

Pease note that the above examples contains some minimal configuration. There are many more properties to use. See the documentation.

  1. Create arquillian.xml file

Create the arquillian.xml under src/test/resources file and set the jboss home property.

<container qualifier="widlfly-managed" default="true">
    <configuration>
        <property name="jbossHome">${jbossHome:target/wildfly-8.2.0.Final}</property>
    </configuration>
</container>

Without this property, when running the test, an error will be thrown stating that the container cannot be started.

  1. Create the arquillian test The arquillian test has some specific configurations in order to make it work.

    • Add the appropriate annotation

To run a test using Arquillian, the test class must be annotated with @RunWith(Arquillian.class)

  • Add the deployment method.

The deployment method’s scope is to package all the code and it’s dependencies and create a archive (war or jar). To differentiate the deployment method from other metods in the test class, @Deployment annotation is used.

@Deployment
public static JavaArchive createDeployment() {
    return ShrinkWrap.create(JavaArchive.class)
            .addClass(MyClass.class)
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

A full test class example

@RunWith(Arquillian.class)
public class MessageDaoTest {
    @Deployment
    public static WebArchive createDeployment() {
        WebArchive webArchive = ShrinkWrap.create(WebArchive.class)
                .addClasses(MessageDao.class, Message.class)
                .addAsResource("persistence.xml", ArchivePaths.create("META-INF/persistence.xml"))
                .addAsResource("insert.sql", ArchivePaths.create("insert.sql"))
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
        System.out.println(webArchive.toString());
        return webArchive;
    }
}

When packaging with Shrinkwrap, resources can be added to the archive. One example is the SQL script used to populate the database. Another example is the persistence.xml configuration file. Please note that the persistence.xml file must be put under META-INF folder.

About

Example project of how to create an Java EE 7 project with persistence

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published