Official Seam demo app for Glassfish

As an application framework for Java EE 5.0, JBoss Seam works on both JBoss and Glassfish application servers (see Riger Kitain and Brian Leonard's blogs). From Seam 1.1, we will officially maintain the Hotel Booking example application for Glassfish. To use it, just go to the examples/glassfish directory, run ant, and then deploy the build/jboss-seam-glassfish.ear application into Glassfish. The application is available at http://localhost:8080/jboss-seam-glassfish. The example is in CVS now and it should be available in distributions from Seam 1.1 CR2.

But of course, the Hotel Booking is just one example. How do you port other Seam apps to Glassfish? Here are how.

Choose the Hibernate JPA

First, let's be clear that we highly recommend you use Hibernate as the JPA (Java Persistence API) provider in Glassfish. By default, Glassfish uses "TopLink Essentials" (a.k.a the watered-down "lesser TopLink") for JPA implementation. It might be fine for basic JPA needs but Seam makes good use of Hibernate specific features such as Hibernate validators and filters etc. In fact, it would be foolish not to use the Hibernate JPA with Seam considerring how easy it is to install Hibernate JPA in Glassfish. :)

By including Hibernate JARs in your EAR, you can enable Hibernate JPA for a single application. Or you can simply copy Hibernate JARs to Glassfish's lib directory and enable Hibernate JPA for all applications. To use the Hibernate JPA, just choose the proper persistence provider in your persistence.xml as we do below.

If you have to use the "lesser TopLink" JPA, we also have a toplink build target in the examples/glassfish project. But be aware that you need to load the database manually for the hotel data since TopLink does not read the import.sql file.

Diff JBoss Glassfish

All the changes from a JBoss deployment to a Glassfish deployment concern the configuration files and library JARs only.

Since Glassfish uses the JSF RI not MyFaces, you should first make sure that the MyFaces phrase listener is turned off. Just comment the following lines out in web.xml

XML:
  1. <!-- MyFaces -->   
  2. <!--   
  3. <listener>       
  4.   <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>   
  5. </listener>   
  6. -->

Glassfish requires you to declare all EJB3 session bean reference names in the web.xml file for the web application to access the beans. This is a rather tedious process. But you have to add the following lines in web.xml for each session bean in your application.

XML:
  1. <ejb-local-ref>       
  2.   <ejb-ref-name>jboss-seam-glassfish/BookingListAction/local</ejb-ref-name>       
  3.   <ejb-ref-type>Session</ejb-ref-type>       
  4.   <local>org.jboss.seam.example.booking.BookingList</local>       
  5.   <ejb-link>BookingListAction</ejb-link>   
  6. </ejb-local-ref>

You also need to tell Seam the session bean naming pattern you just used in web.xml, so that Seam can locate those beans. So, make sure you have the following in components.xml file.

XML:
  1. <core:init jndi-pattern="java:comp/env/jboss-seam-glassfish/#{ejbName}/local" debug="true"/>

In order to use the Hibernate JPA with Glassfish's buid-in JavaDB (Derby database), you need a persistence.xml file like the following.

XML:
  1. <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"
  2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  3.    
  4.    <persistence-unit name="bookingDatabase">
  5.      
  6.       <provider>org.hibernate.ejb.HibernatePersistence</provider>
  7.       <jta-data-source>jdbc/__default</jta-data-source>
  8.       <properties>
  9.        
  10.          <property name="hibernate.dialect"
  11.               value="org.hibernate.dialect.DerbyDialect"/>
  12.          <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
  13.          <property name="hibernate.show_sql" value="true"/>
  14.          
  15.          <property name="hibernate.cache.provider_class"
  16.               value="org.hibernate.cache.HashtableCacheProvider"/>
  17.       </properties>
  18.    </persistence-unit>
  19. </persistence>

Finally, you need to bundle the following JAR files in your EAR in addition to any libary files you already need for JBoss deploment (e.g., jboss-seam-*.jar, facelets JARs, and Ajax4jsf JARs etc.)

CODE:
  1. hibernate*.jar:              Hibernate3, Annotation, EntityManager JARs
  2. thirdparty-all.jar:          Third party JARs for Hibernate JPA outside of JBoss AS
  3. jboss-archive-browsing.jar:  Required for Hibernate EntityManager
  4. commons-beanutils-1.7.0.jar: Required by Seam outside of JBoss AS
  5. commons-digester-1.6.jar:    Required by Seam outside of JBoss AS

That's it. Now enjoy!

8 Responses to “Official Seam demo app for Glassfish”

  1. Carol McDonald Says:

    Here is another Seam Glassfish example: a Sample Application using JSF, Seam, and Java Persistence APIs on Glassfish

    http://weblogs.java.net/blog/caroljmcdonald/archive/2007/07/sample_applicat_1.html

  2. samd Says:

    I am using Hibernate as my JPA Provider but it is not executing my import.sql on deployment.

    How do you get this to work with GlassFish?

  3. Michael Yuan Says:

    Make sure that you have the “hibernate.hbm2ddl.auto” property value set to “create-drop” or “create”.

  4. samd Says:

    I did. Also I have it so the import.sql is packaged so that it gets placed in the root directory after the ear is exploded on deployment. Here is my persistence.xml

    org.hibernate.ejb.HibernatePersistence
    fishermanswharf
    false

  5. samd Says:

    Oops guess I needed to use the code tags.

    org.hibernate.ejb.HibernatePersistence
    fishermanswharf
    false

  6. samd Says:

    Well anyways it looks like the one you posted in this example since it won’t display here.

  7. Venky Says:

    Thanks a lot for this.
    I am a newbie to seam and i wanted to use glassfish for developing a javaee5 application with seam. This helped me out a lot.

  8. webmaster php scripts Says:

    I think most of them are using hibernate one… thanks for detailed one that you have shown to us. Really nice one

Leave a Reply