We’ll be using maven for dependency management and H2 database. Let’s start by adding all the dependencies to the pom.xml.

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.7.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.7.Final</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.3.174</version>
        </dependency>

Here, We’ll be using hibernate as a JPA provider. By convention JPA looks for the configuration file in META-INF/ in the root of the classpath with name persitence.xml. let’s add one for our application.

<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_1_0.xsd"
             version="1.0">
   <persistence-unit name="helloworld">
       
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       
       <properties>
           <!-- auto detection strategy -->
           <property name="hibernate.archive.autodetection" value="class, hbm"/>
           
           <property name="hibernate.show_sql" value="true" />
           <property name="hibernate.format_sql" value="true" />

           <!--Datasource configuration-->
           <property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
           <property name="hibernate.connection.url" value="jdbc:h2:file:~/db/SampleJPA;AUTO_SERVER=TRUE"/>
           <property name="hibernate.connection.username" value="sa"/>
           <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>

           <!--connection pool -->
           <property name="hibernate.c3p0.min_size" value="5"/>
           <property name="hibernate.c3p0.max_size" value="10"/>
           <property name="hibernate.c3p0.max_statements" value="50"/>
           <property name="hibernate.c3p0.idle_test_period" value="30000"/>

           <!--export schema-->
           <property name="hibernate.hbm2ddl.auto" value="create-drop"/>

       </properties>
   </persistence-unit>
</persistence>

Here we are configuring a persitence unit with Hibernate as a JPA provider along with the datasource and connection pool settings. We have also added configuration for exporting the schema to the database at the start of the application. Let’s add the entity class that has been annotated with the annotation provided in the standard javax.persistence package.

@Entity
@Table(name = "MESSAGES")
public class Message {

    @Id
    @GeneratedValue
    @Column(name = "MESSAGE_ID")
    private Long id;

    @Column(name = "MESSAGE_TEXT")
    private String text;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "MESSAGE_NEXT_ID")
    private Message nextMessage;

    public Message(){}

    public Message(String text){
        this.setText(text);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Message getNextMessage() {
        return nextMessage;
    }

    public void setNextMessage(Message nextMessage) {
        this.nextMessage = nextMessage;
    }
}

That’s all we need for a simple JPA application. Let’s put all our code into action with a main class.

public class Main {

    public static void main(String... args){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld");

        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        Message message = new Message("hello JPA world!");
        message.setNextMessage(new Message("this is the next message"));
        em.persist(message);
        tx.commit();
        em.close();

    }
}

That’s all to the application.