Standalone JPA Hello World Example..

Here it is..

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/xsd/maven-4.0.0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>testproject</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <dependencies>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
 
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
 
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.1.0.Final</version>
        </dependency>
 
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.192</version>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>biz.tugay.testproject.TestClass</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

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="testdb" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost:9092/~/h2dbs/testdb"/>
            <property name="hibernate.connection.username" value="sa"/>
            <property name="hibernate.connection.password" value=""/>
            <property name="hibernate.connection.useUnicode" value="true"/>
            <property name="hibernate.connection.characterEncoding" value="utf-8"/>
            <property name="hibernate.show_sql" value="false"/>
        </properties>
    </persistence-unit>
</persistence>

TestTable.java
package biz.tugay.testproject;
 
import javax.persistence.*;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 8/31/2016
 * Time: 1:43 PM
 */
 
@Entity
@Table(name = "TEST_TABLE")
public class TestTable {
 
    private int id;
    private String data;
 
    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }
 
    public void setId(int ID) {
        this.id = ID;
    }
 
    @Basic
    @Column(name = "DATA")
    public String getData() {
        return data;
    }
 
    public void setData(String DATA) {
        this.data = DATA;
    }
}

PersistenceUtil.java
package biz.tugay.testproject;
 
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 8/31/2016
 * Time: 1:43 PM
 */
public class PersistenceUtil {
 
    private static EntityManagerFactory entityManagerFactory;
 
    static {
        entityManagerFactory = Persistence.createEntityManagerFactory("testdb");
    }
 
    public static EntityManager getEntityManager() {
        final EntityManager entityManager = entityManagerFactory.createEntityManager();
        return entityManager;
    }
 
    public static void closeFactory() {
        entityManagerFactory.close();
    }
}

TestClass.java
package biz.tugay.testproject;
 
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import java.util.Scanner;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 8/31/2016
 * Time: 1:31 PM
 */
public class TestClass {
 
    public static void main(String[] args) throws ClassNotFoundException {
 
        Class.forName("biz.tugay.testproject.PersistenceUtil");
 
        while (true) {
            final String inputFromUser = getInputFromUser();
            if (inputFromUser.equals("exit")) {
                PersistenceUtil.closeFactory();
                break;
            }
            storeInDB(inputFromUser);
        }
    }
 
    public static String getInputFromUser() {
        System.out.println("Please enter data to persist in db and hit enter.");
        System.out.println("Type 'exit' to end the program.");
        final Scanner scanner = new Scanner(System.in, "UTF-8");
        final String inputFromUser = scanner.nextLine();
        return inputFromUser;
    }
 
    public static void storeInDB(String data) {
        final EntityManager em = PersistenceUtil.getEntityManager();
        final EntityTransaction transaction = em.getTransaction();
        final TestTable testTable = new TestTable();
        testTable.setData(data);
        transaction.begin();
        em.persist(testTable);
        transaction.commit();
        em.close();
    }
}

Clean Install

In Action

Lets check the database