Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Struts Training Day1 Persistence

Related Tutorials


Share on Google+Share on Google+


The Java Enterprise Edition (J2EE) supports JPA and supply a persistence provider. It uses the following elements to allow persistence management in EJB 3.0.



There are following JPA persistence provider:

The Java Enterprise Edition (J2EE) supports JPA and supply a persistence provider. It uses the following elements to allow persistence management in EJB 3.0.

  1. Persistence: This is the persistence unit that consists declarative meta-data. It describes the relationship of entity class objects to the relational database. For creating the persistence context the EntityManagerFactory uses the meta-data. Which is accessed through the EntityManager.
  2. EntityManagerFactory: It is used for creating an EntityManger to interact with database. An application server is a container and supply this function, but the EntityManagerFactory is required if you are using JPA application-managed persistence. There are following example for creating a EntityManagerFactory:

    import javax.persistence.Persistence;
    import javax.persistence.EntityManagerFactory;

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence_unit_name");

    // paste your code here according to your requirement



  3. EntityManager: This is a resource manger that maintain an active entity objects, which is used by any applications. It manages the OR mapping meta-data and database inaction. The persistence context is represented by an instance of EntityManger. The EntityManager contains two classes: 1) javax.persistence.EntityManagerFactory and 2) javax.persistence.Persistence.  The Persistence class is a public class that retrieve the EntityManagerFactory for a given named persistence unit. There are following example that shows how to create a EntityManger:

    import javax.persistence.EntityManager;

    EntityManager em = emf.createEntityManager();

    // paste your code here according to your requirement


[Note: The injection of the EntityManager supports the following artifacts]

  • Create EJB 3.0 session beans
  • Crete EJB 3.0 message-driven beans
  • Creates a servlets, but injection doesn't support in JSP
  • Creates a main class of the application client

Query: The Java Persistence APIs defines the JPQL (JPA Query Language) that is used to select objects from data source. The JPQL query has an internal namespace which is declared in the from clause of the JPQL query.  In JPQL, we define an arbitrary identifiers for assigning the entities. See the following JPQL query.

Query q = em.createQuery ("SELECT s FROM Student s");

In above query 's' is identifier that is assigned to the entity Student.

[Note: We can use "as" keyword. This is optionally that can be used when declaring an identifier in the "from" clause. Ex: SELECT s FROM s and SELECT s FROM Student AS s is synonymous.]



If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 12, 2008

Related Tutorials

Discuss: Persistence  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image