Example with: JPA + Servlet + Jetty + MySQL + Hibernate as implementation

I changed the configuration from Hibernate to Java Persistence. Here it goes...

This is a follow-up post to this post. You may want to start from there! So, now I removed all the Hibernate concrete dependencies and instead of Session here I will use EntityManager.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay.saqila</groupId>
    <artifactId>saqila</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
 
    <name>saqila Maven Webapp</name>
 
    <dependencies>
 
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
 
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
 
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
        </dependency>
 
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
 
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
 
        <!-- hibernate here -->
 
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.10.Final</version>
        </dependency>
 
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.10.Final</version>
        </dependency>
 
    </dependencies>
 
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
 
 
    <build>
        <finalName>saqila</finalName>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.1.v20140609</version>
                <configuration>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
</project>

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
         version="3.0">
</web-app>

persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_1.xsd"
             version="2.1">
 
    <persistence-unit name="sakila" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/koraytug_sakila"/>
            <property name="hibernate.connection.username" value="koraytug_root"/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.useUnicode" value="true"/>
            <property name="hibernate.connection.characterEncoding" value="utf-8"/>
            <property name="hibernate.current_session_context_class" value="thread"/>
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
 
</persistence>

HibernateUtil is gone, long live PersistenceUtil.java
package biz.tugay.saqila.dao;
/* User: koray@tugay.biz Date: 06/08/15 Time: 18:29 */
 
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
 
public class PersistenceUtil {
 
    private static EntityManagerFactory entityManagerFactory;
 
    public static void buildEntityManagerFactory() {
        if (entityManagerFactory != null) {
            return;
        }
        entityManagerFactory = Persistence.createEntityManagerFactory("sakila");
    }
 
    public static EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }
 
    public static void killEntityManagerFactory() {
        if (entityManagerFactory != null) {
            entityManagerFactory.close();
        }
    }
}

ActorDao.java
package biz.tugay.saqila.dao;
/* User: koray@tugay.biz Date: 06/08/15 Time: 18:37 */
 
import biz.tugay.saqila.model.Actor;
 
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import java.util.List;
 
@SuppressWarnings("unchecked")
public class ActorDao {
 
    public List<Actor> getAllActors() {
        EntityManager em = PersistenceUtil.getEntityManager();
        EntityTransaction transaction = em.getTransaction();
        transaction.begin();
        List<Actor> actors = em.createQuery("FROM Actor").getResultList();
        transaction.commit();
        return actors;
    }
 
    public Actor getWithId(int id) {
        EntityManager em = PersistenceUtil.getEntityManager();
        EntityTransaction transaction = em.getTransaction();
        transaction.begin();
        Actor actor = em.find(Actor.class, id);
        transaction.commit();
        return actor;
    }
 
    public void addNewActor(String first_name, String last_name) {
        Actor actor = new Actor();
        actor.setFirst_name(first_name);
        actor.setLast_name(last_name);
        EntityManager entityManager = PersistenceUtil.getEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        try {
            entityManager.merge(actor);
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
        }
    }
}