Show TOC

Procedure documentationUsing Java Persistence API (JPA) Locate this document in the navigation structure

 

In EJB 3.0 you can access the persistence functionality using the entity manager. The entity manager allows you to synchronize the persistent state of the entities to the database. It manages entity objects in the following way:

  • When you need to retrieve information from database, it returns the respective entities.

  • When you need to update data in the database, you pass the updated entity objects to the entity manager so it can perform the necessary SQL queries for updating the information. In EJB 3.0, you can also perform explicit queries using the SQL-like query language.

JPA provides the Entity Manager API (EM), which is the main API for application developers to interact with the database. The EM manages the life cycle and the state of entities.

You can obtain a container-managed entity manager using session and message-driven beans.

Procedure

You can obtain a container-managed entity manager by the application through dependency injection or through JNDI lookup, or by calling EntityManagerFactory.getEntityManager().

Syntax Syntax

  1. @PersistenceContext
    
    public EntityManager em;
    
End of the code.

The container manages the creation of the entity manager and handles the closing of the entity manager transparently to the application. Entity managers can be injected using the @PersistenceContext annotation. If multiple persistence units exist, the unitName element must be specified. The type element specifies whether a transaction-scoped or extended persistence context is to be used.

Syntax Syntax

  1. @PersistenceUnit
    
    EntityManagerFactory emf;
    
    EntityManager em = emf.createEntityManager();
    
End of the code.

Example

This is an example of a bean-managed transaction demarcation:

Syntax Syntax

  1. package com.sap.example;
    
    
    
    import javax.annotation.Resource;
    
    import javax.ejb.Stateless;
    
    import javax.ejb.TransactionManagement;
    
    import javax.ejb.TransactionManagementType;
    
    import javax.persistence.EntityManager;
    
    import javax.persistence.PersistenceContext;
    
    import javax.transaction.UserTransaction;
    
    
    
    @Stateless
    
    @TransactionManagement(TransactionManagementType.BEAN)
    
    public class StatelessBean implements StatelessLocal {
    
    
    
       @Resource
    
       UserTransaction ut;
    
    
    
       @PersistenceContext
    
       EntityManager em;
    
    
    
       public void businessMethod() {
    
          ut.begin(); // Start a new transaction
    
          try {
    
             // Do work
    
             em.persist(myEntity);
    
             ut.commit(); // Commit the transaction
    
          } catch (Exception e) {
    
             ut.rollback(); // Rollback the transaction
    
          }
    
       }
    
    }
    
End of the code.

This is an example of a container-managed transaction:

Syntax Syntax

  1. package com.sap.example;
    
    
    
    import javax.ejb.Stateless;
    
    import javax.ejb.TransactionAttribute;
    
    import javax.ejb.TransactionAttributeType;
    
    import javax.ejb.TransactionManagement;
    
    import javax.ejb.TransactionManagementType;
    
    import javax.persistence.EntityManager;
    
    import javax.persistence.PersistenceContext;
    
    
    
    @Stateless
    
    @TransactionManagement(TransactionManagementType.CONTAINER)
    
    public class StatelessBean implements StatelessLocal {
    
    
    
       @PersistenceContext EntityManager em; 
    
    
    
       @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    
       public void businessMethod() {
    
          // Work in transaction
    
       }
    
    }
    
End of the code.
Annotation Reference

Name

Use

Target

Annotation Attribute

@Persistence

Context

Use it to express a dependency to an Entity Manager persistence context.

TYPE

METHOD

FIELD

name - refers to the name by which the Entity Manager and its persistence unit are known in the environment referencing context. You do not need this element when you use dependency injection. The default value is "".

unitName - refers to the name of the persistence unit. The default value is "".

type - The default value is TRANSACTION.

@Persistence

Unit

Use it to express a dependency to an EntityManagerFactory.

TYPE

METHOD

FIELD

name - refers to the name by which the Entity Manager and its persistence unit are known in the environment referencing context. You do not need this element when you use dependency injection. The default value is "".

unitName - refers to the name of the persistence unit. The default value is "".