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="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_1_0.xsd">
    <persistence-unit name="HelloJPA1" >
    <jta-data-source>jdbc/hellojpa</jta-data-source>
    <class>com.webspherenotes.jpa.Customer</class>
    <properties>
    <property name="openjpa.jdbc.Schema" value="ADMIN"/>
    </properties>
    </persistence-unit>
    </persistence>


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

    package com.webspherenotes.jpa;

    import java.io.*;
    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{
    super.init();
    }

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

    EntityManagerFactory entityManagerFactory = Persistence
    .createEntityManagerFactory("HelloJPA1");
    CustomerManager manager = new CustomerManager(entityManagerFactory);

    List customerList = manager.getCustomers();
    for(Customer c : customerList){
    System.out.println(c);
    }

    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

8 comments:

  1. 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(DBDictionaryFactory.java:102)
    at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDBDictionaryInstance(JDBCConfigurationImpl.java:603)
    at org.apache.openjpa.jdbc.meta.MappingRepository.endConfiguration(MappingRepository.java:1510)
    at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:518)
    at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:443)
    at org.apache.openjpa.lib.conf.PluginValue.instantiate(PluginValue.java:104)
    at org.apache.openjpa.conf.MetaDataRepositoryValue.instantiate(MetaDataRepositoryValue.java:68)
    at org.apache.openjpa.lib.conf.ObjectValue.instantiate(ObjectValue.java:83)
    at org.apache.openjpa.conf.OpenJPAConfigurationImpl.newMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:967)
    at org.apache.openjpa.conf.OpenJPAConfigurationImpl.getMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:958)

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    Data Science course in Chennai
    Data science course in bangalore
    Data science course in pune
    Data science online course

    ReplyDelete
  4. Well explanation with great coding knowledge. This blog gonna helpful to many. I am expecting these kind blogs in future too.Websphere Application Server Training in Bangalore

    ReplyDelete