• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How To Create EntityManagerFactory in JPA

December 28, 2013 //  by Krishna Srinivasan//  Leave a Comment

This tutorial explains how to create EntityManagerFactory in your JPA application when you are writing your first JPA programming. It is the standard way of creating the factory if you use the JPA specification. Normally there are two ways to create the factory, one is through the configuration file persistence.xml and another one is through the application server configurations. Here I am going to explain about the first approach how to writing in your programming. You have to follow these steps for creating your EntityManagerFactory.

  1. Create persistence.xml file under the classpath. Make sure that you keep this file under META-INF folder. For example, your source files are maintained in the folder “src”, then create a META-INF folder under “src”.
  2. Another important point is to add the persistence unit name in the configuration file. This one will be used while creating the EntityManagerFactory. Note that each factory will have a name. You can have multiple factories with multiple configuration files. If you are not specifying the unit name, you would get the  javax.persistence.PersistenceException: No Persistence provider for EntityManager named JPAUNIT
  3. Create the factory by passing the persistence unit name as the parameter.
  4. I assume that you have setup the project with all the required files for Java Persistence API (JPA).

Lets see the sample code:

persistence.xml

[code lang=”xml”] <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="JPAUNIT">
<provider>javax.persistence.spi.PersistenceProvider</provider>
<class>javabeat.net.hibernate.Employee</class>
<properties>
<property name="connection.jdbc.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="connection.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="connection.jdbc.user" value="root"/>
<property name="connection.jdbc.password" value="admin"/>
</properties>
</persistence-unit>
</persistence>
[/code]

HibernateUtil.java

[code lang=”java”] package javabeat.net.hibernate;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class HibernateUtil {
public static void main(String args[]){
EntityManagerFactory entityManager = Persistence.createEntityManagerFactory("JPAUNIT");
EntityManager entityManager2 = entityManager.createEntityManager();
EntityTransaction transaction = entityManager2.getTransaction();
transaction.begin();
Employee employee = new Employee();
employee.setEmpName("Senthil Kumar1");;
employee.setBranch("Pune");
entityManager2.persist(employee);
transaction.commit();
}
}
[/code]

Category: HibernateTag: Hibernate 4 Tutorials, JPA 2 Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « JPA EntityManagerFactory Vs Hibernate’s SessionFactory
Next Post: JPA Releases and Implementations »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact