Sunday, June 5, 2011

JSF JPA with Glassfish and OEPE

The marriage between Oracle and Sun had been for a while. One of the blessings out of this is the integration between Glass fish and OEPE(Eclipse)IDE. This gives opportunity for eclipse cult to try glashfish from their yard. I would recommend watching this youtube series to get a head start You Tube URL. I spent some time on this to see how easy to list values from a table in database using this mix. I will use the JPA and JSF standards. My experiment resulted in a very simple web app with clean code.

Download URL’s
OEPE Download URL (you will get eclipse ide customized for WebLogic and glassfish runtimes yes it is free)
Glassfish Download URL(you will get JEE6 complaint app server open source as well as commercial version)


First let us start with database and it is going to be mysql and this is table. I will use the Database Development perspective of OEPE if you are not really a DBA I don’t think you need to have another process running in your box just to manage database vide Quick Start URL , My table as seen in OEPE DB perspective

  


Next step is setup the IDE for development which is a two step process
1.       Create Glassfish runtime  
a.       Windows à show view à other àServers
b.      Right click in the servers tab ,in the popup menu pick new server – pick the open source glassfish runtime
PS this video has a demo of http://www.youtube.com/watch?feature=player_detailpage&v=9Kf5m7bMu74
2.       Create a project type of type dynamic web app
a.       Go to project explorer right click create a new project Web ---Dynamic web app name it as data pull

This will be the resulting IDE display  





Now as stated earlier we will be using JSF 2.0 and JA 2.0 for that we need to enable those facets, right click project properties and pick facets this video has a demo http://www.youtube.com/watch?v=CuLYhkqt0V0&feature=player_detailpage#t=492s
After your successful effort you will see the screen shot but not red rectangles that is for you to make sure they are enabled








Now the code part
Create a package named model under the src folder of the project and right click and select Entities from table and pick the user table 
 
The above steps creates the user entity table in a flash, configure a data source in the glassfish server with the jndi name jdbc/DataSource1 to your schema
Make sure your persistence.xml points to it
Resulting 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="dataPull" transaction-type="JTA">
            <jta-data-source>jdbc/DataSource1jta-data-source>
            <class>model.Userclass>
      <persistence-unit>
<persistence>



OEPE design time will help you so that you don’t need to manually edit the file
Now let us create the jsf page which acts as view for the data, below is the code snippet of the same

        <h:dataTable value="#{userBean.userList}" var="item">
            <h:column>
                <f:facet name="header">
                    <h:outputText value="UserName"/>
                f:facet>
                <h:outputText value="#{item.userName}"/>
            h:column>
           
        h:dataTable>

If you observe closely you will see the reference to the managed bean Userbean which act as a controller see the below code, now this I will create under a new package controller. From JSF 2.0 you don’t need to define your managed bean in the faces-config.xml it is taken care by annotation. Let us look some code snippets
@ManagedBean(name="userBean")
@SessionScoped
public class UserBean {
      @PersistenceUnit(unitName = "home_app")
    private EntityManagerFactory emf;

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
      public String displayUser(){
            initial = false
             
            EntityManager em = getEntityManager();
             
            Query query = em.createNamedQuery("findAllUsers");
            this.userList = query.getResultList();
            System.out.println("Size Matters"+ userList.size());
       
            return "users";
      }
Notice it is easy to inject the persistence unit into this program to talk to the jpa layer. We simply pass the user list and dataTable in JSF takes care of the dynamic result set.
Where is the query?? It is defined in ORM.xml
<named-native-query name="findAllUsers" result-class="model.User">
    <query>select * from User cquery>
    named-native-query>
Creating ORM.xml is easy in OEPE New->JPA->Mapping file … Trust me you won’t go wrong , after that you are all set to go ,execute the users.jsp you should see it working fine
The source and war can be accessed here it is 9 kb App URL can you believe the size for what it does. Step back and think throwing in spring and other stuff to do this you know what you will end up with.










No comments:

Post a Comment