Tutorial Using the Java Persistence API (JPA) in Hibernate

By: Felix Emailed: 1784 times Printed: 2615 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This tutorial is located within the download bundle under entitymanager and illustrates using annotations to provide mapping information using JPA

persistence.xml

Instead of using the Hibernate-specific hibernate.cfg.xml configuration file, JPA, defines a different bootstrap process that uses its own configuration file named persistence.xml. How this bootstrapping works is defined by the JPA specification. In Java™ SE environments the persistence provider (Hibernate in this case) is required to locate all JPA configuration files by classpath lookup of the META-INF/persistence.xml resource name.

<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="org.hibernate.tutorial.jpa">
...
</persistence-unit>
</persistence>

persistence.xml files should provide a unique name for each persistence unit. This name is how applications reference the configuration while obtaining an javax.persistence.EntityManagerFactory reference.

Here  the javax.persistence-prefixed varieties are used when possible. For the remaining Hibernate-specific configuration setting names notice that they are now prefixed with hibernate..

The annotated entity Java class

This tutorial uses the JPA APIs instead of the Hibernate APIs.

Example: Obtaining the javax.persistence.EntityManagerFactory

protected void setUp() throws Exception {
entityManagerFactory = Persistence.createEntityManagerFactory( "org.hibernate.tutorial.jpa" );
}

Notice again the use of org.hibernate.tutorial.jpa as the persistence unit name, which matches from Example 4.1, “persistence.xml”

Example: Saving (persisting) entities

EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist( new Event( "Our very first event!", new Date() ) );
entityManager.persist( new Event( "A follow up event", new Date() ) );
entityManager.getTransaction().commit();
entityManager.close();

Here we use an javax.persistence.EntityManager as opposed to a org.hibernate.Session. JPA calls this operation persist instead of save.

Example: Obtaining a list of entities

entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
List<Event> result = entityManager.createQuery( "from Event", Event.class ).getResultList();
for ( Event event : result ) {
System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
}
entityManager.getTransaction().commit();
entityManager.close();


Hibernate Home | All Hibernate Tutorials | Latest Hibernate Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Felix
Hibernate JDBC and Connection Properties
Hibernate Transaction Properties
Hibernate Cache Properties
Hibernate Configuration Properties
Creating Connection Pool for JDBC Connections in Hibernate
SessionFactory in Hibernate
Programmatic configuration in Hibernate
Contextual sessions in Hibernate
Some Basic APIs in Hibernate
A sample Hibernate Web Application using Servlets
Step by Step Hibernate - Your First Hibernate Application
Tutorial Using the Java Persistence API (JPA) in Hibernate
Download Hibernate latest version

More Tutorials in Hibernate
Fetching strategies in Hibernate
equals() and hashCode() in Hibernate
Tuplizers (org.hibernate.tuple.Tuplizer) in Hibernate
EntityNameResolvers in Hibernate
Hi/lo algorithm in Hibernate
Identity columns and sequences in Hibernate
Assigned identifiers in Hibernate
Primary keys assigned by triggers in Hibernate
Formula in Hibernate
Step by Step Hibernate - Your First Hibernate Application
A sample Hibernate Web Application using Servlets
Some Basic APIs in Hibernate
Contextual sessions in Hibernate
Programmatic configuration in Hibernate
SessionFactory in Hibernate

More Latest News
Most Viewed Articles (in Hibernate )
Hibernate Vs. JDBC ( A comparison)
A sample Hibernate Web Application using Servlets
Fetching strategies in Hibernate
Step by Step Hibernate - Your First Hibernate Application
Download Hibernate latest version
Hibernate Configuration Properties
Programmatic configuration in Hibernate
Creating Connection Pool for JDBC Connections in Hibernate
Contextual sessions in Hibernate
Hibernate JDBC and Connection Properties
Hi/lo algorithm in Hibernate
Hibernate Cache Properties
Hibernate Transaction Properties
Assigned identifiers in Hibernate
Identity columns and sequences in Hibernate
Most Emailed Articles (in Hibernate)
Contextual sessions in Hibernate
Programmatic configuration in Hibernate
Hibernate Vs. JDBC ( A comparison)
Step by Step Hibernate - Your First Hibernate Application
Types of inheritence models in Hibernate
Hibernate Transaction Properties
Fetching strategies in Hibernate
EntityNameResolvers in Hibernate
Formula in Hibernate
Delta Electronics Chairman Bruce Cheng Endows NCKU Build Y.S. Sun Green Building Research Center
Paging Through the Result Set in Hibernate
SessionFactory in Hibernate
Hibernate Configuration Properties
equals() and hashCode() in Hibernate
Primary keys assigned by triggers in Hibernate