Hibernate with Java Persistence API (JPA)

pom.xml

Add Hibernate entity manager to Maven POM (pom.xml) for Java Persistence API (JPA)

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>${project.version}</version>
</dependency>

JPA configuration file - persistence.xml

persistence.xml is the equivalent of hibernate.cfg.xml in JPA

<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="com.innotrekker.api.jpa">
        <description>
            Hibernate using JPA
        </description>

        <class>com.innotrekker.app.entity.Order</class>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@myhost:1521:mydb" />
            <property name="javax.persistence.jdbc.user" value="myuser" />
            <property name="javax.persistence.jdbc.password" value="secret" />

            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="create" />
        </properties>

    </persistence-unit>

</persistence>

Hibernate Entity Java Class using JPA

JPA Persistent Entity Class

package com.innotrekker.app.em;

import java.util.Date;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table( name = "ORDERS" )
public class Order {
    private Long id;

    private String comment;
    private Date date;

    public Order() {
    }

    public Order(String comment, Date date) {
	this.comment = comment;
	this.comment = comment;
    }

    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy = "increment")
    public Long getId() {
	return id;
    }

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

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "o_time")
    public Date getDate() {
	return date;
    }

    public void setDate(Date date) {
	this.date = date;
    }

    @Column(name = "o_comment")
    public String getComment() {
	return comment;
    }

    public void setComment(String comment) {
	this.comment = comment;
    }
}

Using Hibernate JPA API

entityManagerFactory = Persistence.createEntityManagerFactory("com.innotrekker.api.jpa" );
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist( new Order( "Gift wrapped", new Date() ) );
entityManager.getTransaction().commit();
entityManager.close();

entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
List<Order> results = entityManager.createQuery( "from Order", Order.class ).getResultList();
for ( Event event : result ) {
       ...
}
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();

For simplicity, all exceptional handle and final block are not included for illustration purpose