Standalone JPA Example with Hibernate Implementation..

Turns Java Logging OFF, uses h2db in server mode and comes with Silent Maven Exec Plugin!

Directory Layout

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</groupId>
    <artifactId>jpa-examples</artifactId>
    <packaging>jar</packaging>
 
    <version>1.0-SNAPSHOT</version>
 
    <name>jpa-examples</name>
    <url>http://www.tugay.biz</url>
 
    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.10.Final</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.192</version>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>jpa-examples</finalName>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>biz.tugay.jpaExamples.App</mainClass>
                    <commandlineArgs>-Dfile.encoding=UTF-8</commandlineArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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="jpaExamples" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>biz.tugay.jpaExamples.model.Person</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:~/sampleh2db;TRACE_LEVEL_FILE=0;AUTO_SERVER=TRUE"/>
            <property name="javax.persistence.jdbc.user" value="test"/>
            <property name="javax.persistence.jdbc.password" value="test"/>
        </properties>
    </persistence-unit>
</persistence>

Person.java
package biz.tugay.jpaExamples.model;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
 
@Entity
public class Person {
 
    private long id;
    private String firstname;
    private String lastname;
    private String address;
    private String city;
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getFirstname() {
        return firstname;
    }
 
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
 
    public String getLastname() {
        return lastname;
    }
 
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
    public String getCity() {
        return city;
    }
 
    public void setCity(String city) {
        this.city = city;
    }
 
    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", firstname='" + firstname + '\'' +
                ", lastname='" + lastname + '\'' +
                ", address='" + address + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}

Person.java
package biz.tugay.jpaExamples;
 
import biz.tugay.jpaExamples.model.Person;
 
import javax.persistence.*;
import java.sql.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class App {
 
    static {
        final Logger globalLogger = Logger.getLogger("");
        globalLogger.setLevel(Level.OFF);
    }
 
    private static final String PERSISTENCE_UNIT_NAME = "jpaExamples";
    private static final EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
 
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        final EntityManager entityManager = ENTITY_MANAGER_FACTORY.createEntityManager();
 
        final TypedQuery<Person> selectAllPersons = entityManager.createQuery("SELECT p FROM Person p", Person.class);
        final List<Person> persons = selectAllPersons.getResultList();
 
        for (Person person : persons) {
            System.out.println(person);
        }
 
        entityManager.close();
 
        ENTITY_MANAGER_FACTORY.close();
    }
}

Build & Run