Developing JPA application for use in WebSphere

In the Developing JPA application for use in Java SE entry i blogged about how to develop a simple JPA application using RAD and execute it outside the application server. But the more common use case for JPA would be how to use it inside Application Server.

I wanted to learn how to develop an JPA application that runs inside WebSphere Application Server so i build this simple HelloJPA1Portlet application which is essentially same as that of the HelloJPA1 application that i built in the In the Developing JPA application for use in Java SE , with difference that this one runs inside in application container.

Follow these steps to add support for JPA inside a portlet

  1. First create a HelloJPA1Portlet in RAD and deploy it in the WPS 7.0. Once the basic thing is working you can right click on the project and click on Properties. Select Project Facets like this

    Select JPA and version 1.0

  2. Next configure a JDBC Datasource connecting to the same database that we used in the last project and use jdbc/hellojpa as the JNDI name for the data source make sure the database actually works

  3. Follow the same steps that i mentioned in Developing JPA application for use in Java SE for generating entities accessing Customer table or you might want to simply copy the code from there.

  4. One step that is different is change persistence.xml to use the data source configured at jdbc/hellojpa JNDI location instead of directly connecting to database. You can do that by right clicking on project and then selecting JPA Tools -< Configure project for JDBC Deployment

    As you can see i am using the JNDI name of the data source instead of database properties

  5. When you click on the Finish button, your persistence.xml file will get updated to look like this

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.0" xmlns=""
    <persistence-unit name="HelloJPA1" >
    <property name="openjpa.jdbc.Schema" value="ADMIN"/>

  6. Your portlet code would be same as that of the standalone client like this

    package com.webspherenotes.jpa;

    import java.util.List;

    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    import javax.portlet.*;

    import com.webspherenotes.jpa.controller.CustomerManager;

    * A sample portlet
    public class HelloJPA1Portlet extends javax.portlet.GenericPortlet {
    * @see javax.portlet.Portlet#init()
    public void init() throws PortletException{

    * Serve up the view mode.
    * @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest,
    public void doView(RenderRequest request, RenderResponse response)
    throws PortletException, IOException {
    // Set the MIME type for the render response

    EntityManagerFactory entityManagerFactory = Persistence
    CustomerManager manager = new CustomerManager(entityManagerFactory);

    List customerList = manager.getCustomers();
    for(Customer c : customerList){

    super.getPortletContext().getRequestDispatcher("/index.jsp").include(request, response);


Now when you try to access the portlet you should be able to see list of customers being printed in the SystemOut.log file


Uday kumar said...

Hi Sunil,
I am getting the below error while connecting to a SQL datasource configured in websphere server. I have provided the user name and password in the J2C authentication alias but somehow this user name is not being picked. Please advise on what could be the issue

org.apache.openjpa.persistence.PersistenceException: Login failed for user ''. DSRA0010E: SQL State = S0001, Error Code = 18,456
at org.apache.openjpa.jdbc.sql.DBDictionaryFactory.newDBDictionary(
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDBDictionaryInstance(
at org.apache.openjpa.jdbc.meta.MappingRepository.endConfiguration(
at org.apache.openjpa.lib.conf.Configurations.configureInstance(
at org.apache.openjpa.lib.conf.Configurations.configureInstance(
at org.apache.openjpa.lib.conf.PluginValue.instantiate(
at org.apache.openjpa.conf.MetaDataRepositoryValue.instantiate(
at org.apache.openjpa.lib.conf.ObjectValue.instantiate(
at org.apache.openjpa.conf.OpenJPAConfigurationImpl.newMetaDataRepositoryInstance(
at org.apache.openjpa.conf.OpenJPAConfigurationImpl.getMetaDataRepositoryInstance(

prathap kumar said...

Interesting Article

Spring Hibernate Training Online | Hibernate Training in Chennai

Hibernate Training Online | Java Online Course

Ancy merina said...

A significant great blog by the creator with loads of instructive post on website architecture and improvement. A debt of gratitude is in order for share quality data with us.
Web Development Company In Bangalore | Web Designing Companies In Bangalore