Java Persistence API (JPA)

The JPA can be used to create object oriented versions of relational database entities for developers to make use of in their applications.

You can use the JPA to provide annotations and XML extensions which you can use to describe tables in their database and their contents, including data types, keys and relationships between tables. Developers can use the API to perform database operations instead of using SQL.

CICS supports jpa-2.0, jpa-2.1 and jpa-2.2. For information on how these versions differ, see Java Persistence API (JPA) feature overview.

  • Entity objects are simple Java classes, and can be concrete or abstract. Each represents a row in a database table, and properties and fields are used to maintain states. Each field is mapped to a column in the table, and key information about that particular field is added in; for example, you can specify primary keys, or fields that can't be null.
    @Entity
    @Table(name = "JPA")
    public class Employee implements Serializable
    {
        @Id
        @Column(name = "EMPNO")
        private Long EMPNO;
    
        @Column(name = "NAME", length = 8)
        private String NAME;
    
        private static final long serialVersionUID = 1L;
    
        public Employee()
        {
            super();
        }
    
        public Long getEMPNO()
        {
            return this.EMPNO;
        }
    
        public void setEMPNO(Long EMPNO)
        {
            this.EMPNO = EMPNO;
        }
    
        public String getNAME()
        {
            return this.NAME;
        }
    
        public void setNAME(String NAME)
        {
            this.NAME = NAME;
        }
    }
  • The EntityManagerFactory is used to generate an EntityManager for the persistence unit. EntityManager maintains the active collection of entity objects being used by an application. You can use the EntityManager class to initialize the classes and create a transaction for managing data integrity. Next, you interact with the data using the Entity class get and set methods, before using the Entity transaction to commit the data.

    The following example contains sample code to insert a record:

    @WebServlet("/Create")
    public class Create extends HttpServlet
    {
        private static final long serialVersionUID = 1L;
    
        @PersistenceUnit(unitName = "com.ibm.cics.test.wlp.jpa.annotation.cics.datasource")
        EntityManagerFactory emf;
    
        InitialContext ctx;
    
        /**
        * @throws NamingException
        * @see HttpServlet#HttpServlet()
        */
        public Create() throws NamingException
        {
            super();
            ctx = new InitialContext();
        }
    
        /**
        * @see HttpServlet#doGet(HttpServletRequest request,
        HttpServletResponse response)
        */
        protected void doGet(HttpServletRequest request, HttpServletResponse
            response) throws ServletException, IOException
        {
            // Get the servlet parms
            String id = request.getParameter("id");
            String name = request.getParameter("name");
    
            // Create a new employee object
            Employee newEmp = new Employee();
            newEmp.setEMPNO(Long.valueOf(id));
            newEmp.setNAME(name);
    
            // Get the entity manager factory
            EntityManager em = emf.createEntityManager();
    
            // Get a user transaction
            UserTransaction utx;
    
            try
            {
                // Start a user transaction and join the entity manager to it
                utx = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
                utx.begin();
                em.joinTransaction();
    
                // Persist the new employee
                em.persist(newEmp);
    
                // End the transaction
                utx.commit();
            }
            catch(Exception e)
            {
                throw new ServletException(e);
            }
            
            response.getOutputStream().println("CREATE operation completed");
        }
    }
  • @PersistenceUnit expresses a dependency on an EntityManagerFactory and its associated persistence unit. The name of the persistence unit as defined in the persistence.xml file. To connect the entities and tables to a database we then create a persistence.xml file in our bundle. The persistence.xml file describes the database that these entities connect to. The file includes important information such as the name of the provider, the entities themselves, the database connection URL and drivers.

    The following example contains a sample persistence.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0"
        xmlns="http://java.sun.com/xml/ns/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">
    
        <persistence-unit name="com.ibm.cics.test.wlp.jpa.annotation.cics.datasource">
            <jta-data-source>jdbc/jpaDataSource</jta-data-source>
            
            <class>com.ibm.cics.test.wlp.jpa.annotation.cics.datasource.entities.Employee</class>
            
            <properties>
                <property name="openjpa.LockTimeout" value="30000" />
                <property name="openjpa.Log" value="none" />
                <property name="openjpa.jdbc.UpdateManager" value="operation-order" />
            </properties>
        </persistence-unit>
    </persistence>