Accessing data using JPA in a different bundle

About this task

You need to configure your JPA bundle and add the data sources to WebSphere® Application Server before you can access JPA persistence units from another bundle. For example, a web application bundle that consumes JPA entities and displays and manipulates data.
Tip:

Ensure that your JPA persistence file contains references to a Java™ Transaction API (JTA) and non-JTA data source.

JPA has two transactional patterns for accessing a data source:
jta-data-source
The Java Transaction API (JTA) resource pattern depends on global transactions. The JTA resource pattern is typically used within the scope of an Enterprise JavaBeans (EJB) session facade. This configuration allows the session bean to control transaction and security contexts while JPA handles the persistence mappings. In this case, the application does not use the EntityTransaction interface but relies on the EntityManager enlisted with the global transaction when it is accessed.
non-jta-data-source
The non-JTA resource pattern is used to deal with a single resource in the absence of global transactions. The non-JTA resource pattern is typically used within the scope of a web application or an application client. The application controls the transaction with the data source with the EntityTransaction interface.

In persistence.xml files for an OSGi application, the jta-data-source and non-jta-data-source elements access the data sources through a Java Naming and Directory Interface (JNDI) lookup, a JNDI lookup to the service registry, or through Blueprint.

If the JTA and non-JTA data sources are not configured in the persistence.xml file, the default JTA and non-JTA data sources configured for the server are used. By default the values are null. Some JPA entity features require a non-JTA data source to be specified. For example, automatic entity identity generation.

Procedure

  1. Ensure that the entity and entity controller packages are added to manifest.mf as export packages:
    1. Double-click Manifest: <project_name>, where <project_name> is the name of your JPA bundle project. The bundle manifest opens in the editor.
    2. Switch to the Runtime tab. Verify that the entities and entity controller packages are added as export packages.
  2. Add non-JTA data sources to persistence.xml:
    1. Open persistence.xml in the editor.
    2. In the components list, select your entity to display the details of your entity.
    3. In the Non-JTA data source field, type the global JNDI name of a non-datasource. For example, osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/blogdbnojta).
  3. Modify the getEntityManager() method.

    By modifying the getEntityManager() method, you are configuring the JNDI lookup for the EntityManagerFactory service since the persistence unit in a JPA bundle is not in a Java EE environment.

    You can also configure the JNDI lookup by making the entity manager bean a blueprint managed bean. For more information, see JPA and OSGi Applications.

    1. Open the entity manager bean in the editor.
    2. Locate getEntityManager() and modify it as follows:
      private EntityManager getEntityManager() {
      		try {
      			emf = (EntityManagerFactory) new InitialContext().lookup("osgi:service/javax.persistence.EntityManagerFactory/(osgi.unit.name=jpaBundle)");
      		} catch (NamingException e) {
      			// TODO Auto-generated catch block
      			e.printStackTrace();
      		}
      		
      		return emf.createEntityManager();
      	}
      Important: In the following line of code, ensure that the persistence unit matches the persistence unit in persistence.xml:
      emf = (EntityManagerFactory) new InitialContext().lookup("osgi:service/javax.persistence.EntityManagerFactory/(osgi.unit.name=jpaBundle)");
  4. Fix generated errors on InitialContext and NamingException:
    1. Switch to the Markers view.
    2. For each error, right-click the error and select QuickFix. Follow the instructions in the wizard to import the required packages.
  5. Add a JDBC provider to the WebSphere Application Server administrative console:
    1. Switch to the Servers view.
    2. Right-click your server instance and select Start.
    3. Right-click your server instance and select Administration > Run Administrative Console to open the administrative console.
    4. Click Resources > JDBC > JDBC providers.
    5. Click New in the JDBC providers page. The Create a data source wizard opens.
    6. Follow the instructions in the wizard to create the JDBC provider.
    7. Save your changes,
  6. Add data source definitions to the WebSphere Application Server admin console:
    1. In the administrative console, click Resources > JDBC > Data sources to open the data sources page in the console.
    2. In the Data sources page, click New to create a data source definition with a JNDI name set to the JTA connection definition specified in persistence.xml. For example, jdbc/blogdb.
    3. In the Data sources page, click New to create another data source definition with a JNDI name set to the non-JTA connection definition specified in persistence.xml. For example, jdbc/blogdbnojta.
    4. After the data source for the non-JTA connection is created, click the definition for the non-JTA connection in the Data sources page of the admin console. The Configuration page opens.
    5. In the Additional Properties section, click WebSphere Application Server data source properties.
    6. Select Non-transactional data source. Within the application server, the use of the <non-jta-data-source> element requires a special configuration for a non-transactional data source. Data sources that are configured for the application server do not function as a <non-jta-data-source> because all data sources that are configured by the application server are automatically enlisted with the current transactional context. To prevent this automatic enlistment, add an additional data source custom property nonTransactionalDataSource=true.

Results

You can now consume JPA entities, display, and manipulate JPA data, where a JPA bundle is accessed from a web application bundle.
Icon that indicates the type of topic Task topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: taccessjpaoutbundle.html