tumblr counter

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

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">
		<class>com.test.jpa.Student</class>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@192.148.27.140:1521:database"/>
			<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"/>
		</properties>
	</persistence-unit>
</persistence>



1 2 3 Next





4 Responses to “Java Persistence (JPA) Tutorial With Hibernate”
  1. Audwit 23.11.2014 11:51:10
  1. admin 25.11.2014 07:22:04
  1. Prateek 11.01.2015 22:55:16
  1. admin 13.01.2015 07:35:31

Your email address will not be published. Required fields are marked *