tumblr counter

Java Persistence (JPA) Tutorial With Hibernate

Java Persistence (JPA) Tutorial With Hibernate

JPA Tutorial With Hibernate explains step by step details of setting / configuring Java Persistence (JPA) With Hibernate And Eclipse

How To Configure Hibernate (JPA) With Oracle Database?

Java Persistence API (JPA), is a standard interface which wraps different ORM tools such as  Hibernate, EclipseLink, OpenJPA etc. The benefits of using JPA is that you can change the underlying implementation of without changing any code.

ie; you can able to change Hibernate implementation to EclipseLink implementation without changing the code base.

On this standalone JPA Example, we are using Java Persistence (JPA) With Hibernate

Hibernate is popular open source ORM (Object Relation Mapping) tool for Java platform, for mapping an entity to a traditional relational database like Oracle, MySQL etc

By using JPA, we can do database operations like Create, Read, Update & Delete etc, with very little code.

Required Libraries

You need to download

  1. JDK 6
  2. Eclipse 3.7
  3. Hibernate 4.2.1

Following jar must be in classpath

  1. antlr-2.7.7.jar
  2. classes12.jar (Oracle Driver)
  3. dom4j-1.6.1.jar
  4. hibernate-commons-annotations-4.0.2.Final.jar
  5. hibernate-core-4.2.2.Final.jar
  6. hibernate-entitymanager-4.2.2.Final.jar
  7. hibernate-jpa-2.0-api-1.0.1.Final.jar
  8. javassist-3.15.0-GA.jar
  9. jboss-logging-3.1.0.GA.jar
  10. jboss-transaction-api_1.1_spec-1.0.1.Final.jar

I am creating a sample project that persists Student object (simple POJO (Plain Old Java Object)) into database using JPA

Firstly create a Java Project (File->New->Project), select Java Project and click next, provide name as "JPA2Example" according to following screenshot

Create JPA Project Java Persistence(JPA) With Hibernate


persistence.xml file must be under src/META-INF (Please check below screenshot(project structure)

<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" xmlns="http://java.sun.com/xml/ns/persistence">
	<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
			<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@"/>
			<property name="javax.persistence.jdbc.user" value="username"/>
			<property name="javax.persistence.jdbc.password" value="password"/>
			<property name="hibernate.connection.pool_size" value="1"/>
			<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
			<property name="show_sql" value="true"/>
			<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>

Java Persistence Tutorial With Hibernate JPA 2.0 Tutorial With Hibernate And Eclipse

You might also like following tutorials :

Java Tutorial Apache CXF Tutorial Hibernate Tutorial Spring Tutorial Struts Tutorial
4 Comments on "Java Persistence (JPA) Tutorial With Hibernate"
23.11.2014 11:51:10 Audwit
The tutorial could have been really great but the problems I have faced are
1. Could not find the download link of this sample project
2. Could not find sql script

Lesson: Next time I'll remember to download and run before I start a tutorial.
25.11.2014 07:22:04 admin

Can you tell me, what went wrong because I will help you.
11.01.2015 22:55:16 Prateek

I'm running this code, but I'm getting the following error. Could anyone please help me out here ?

INFO: HHH000318: Could not find any META-INF/persistence.xml file in the classpath
Initial SessionFactory creation failed.javax.persistence.PersistenceException: No Persistence provider for EntityManager named test
Exception in thread main java.lang.ExceptionInInitializerError
	at com.test.jpa.EntityManagerUtil.(EntityManagerUtil.java:16)
	at com.test.jpa.JPAExample.(JPAExample.java:12)
	at com.test.jpa.JPAExample.main(JPAExample.java:15)
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named test
	at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
	at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
	at com.test.jpa.EntityManagerUtil.(EntityManagerUtil.java:12)
	... 2 more
13.01.2015 07:35:31 admin

The issue is META-INF/persistence.xml is not found. Please check the Package Structure screen shot, it must be available in the classpath.