Seam-gen on Glassfish – part III

In this last part we’re gonna create the seam-gen project, import it in NetBeans, deploy it and at the end we have a running application.

1. Create the project

Creating the skeleton of the seam-gen project is done by issuing the following seam-gen command

$ ./seam create-project

If you stuck to the defaults there will be a project directory called open18 in your home directory.

2. Generate the JPA classes

Next we’ll generate the JPA classes from the tables in the open18 database into the new project. Again this is done by a seam-gen command

$ ./seam generate

3. Import in NetBeans

Now let’s import the project into NetBeans. It should look like the picture below

open18 project

The build targets for the NetBeans project are coupled to the default (i.e. JBoss) deploy targets. Let’s change them to their Glassfish counterparts. Right click on the open18 project and choose properties. Change the targets so they’ll look like this

open 18 build properties

4. Change build-glassfish.xml

This is the build file that contains all the Glassfish specific Ant targets. One thing it does, is copying all the Hibernate files to the lib/ext folder of the Glassfish domain (Glassfish uses EclipseLink as its default JPA provider).

We’re gonna make 2 changes to this file.

First we’re gonna make sure that the slf4j/log4j libraries also get copied to the Glassfish domain. If you omit this step, the application won’t deploy properly. Add the following xml stanza to the gf-deploy-hibernate target in the glassfish-build.xml file so that the copy task which copies the libraries to Glassfish resembles the example below.

        <copy todir="${}/${glassfish.domain}/lib/ext" overwrite="true">
            <fileset dir="${basedir}/lib">
                <!-- Include slf4j/log4j JARs below -->
                <include name="jcl-over-slf4j.jar"/>
                <include name="slf4j-api.jar"/>
                <include name="slf4j-log4j12.jar"/>
                <include name="log4j.jar"/>

Next let’s fix a small bug in the deployment of the libraries to a Glassfish v2 server. If you don’t change this the asm.jar, asm-attrs.jar, cglib.jar and cglib-nodep.jar won’t be copied into the Glassfish domain.

Alter the lines in the gf-deploy-hibernate target that check whether the Glassfish server is a version 2  or 3.

<condition property="glassfish.v3" value="true" else="false">
    <available file="${glassfish.home}/glassfish" type="dir"/>
<condition property="" value="${glassfish.home}/glassfish/domains" else="${glassfish.home}/domains">
    <istrue value="${glassfish.v3}"/>

5. Prepare Glassfish for Hibernate

Now let’s copy all those library files to the Glassfish domain. Right click the build.xml file in the NetBeans project and choose Run Target > gf-prepare.

One last thing that needs to be taken care of, is fixing a bug in the Seam code regarding Hibernate and mysql. If you don’t do this, the facelet TeeList.xhtml (which corresponds with the Tee List menu item in the open18 application) will give you the following error message

/TeeList.xhtml: javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not execute query

A nice workaround is provided here. In this workaround the flawed Seam component org.jboss.seam.persistence.persistenceProvider is overriden with the following Seam component (which has a higher precedence)

package org.open18.persistence;

import javax.annotation.PostConstruct;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.log.Log;
import org.jboss.seam.log.Logging;
import org.jboss.seam.persistence.HibernatePersistenceProvider;

 * Addresses problem with HibernatePersistenceProvider by explicitly
 * calling HibernatePersistenceProvider.init() from
 * a PostConstruct method that actually gets called on postconstruct.
@Install(classDependencies={"org.hibernate.Session", "javax.persistence.EntityManager"})
public class MoBetterHibernatePersistenceProvider extends HibernatePersistenceProvider {

    private static Log log = Logging.getLog(MoBetterHibernatePersistenceProvider.class);

    public void init()
       log.debug("MoBetterPersistenceProvider init");


6. Deploy the application

We’re almost there. Let’s deploy the application.

  • Right click the build.xml file and choose Run Target > gf-start to startup the Glassfish server;
  • Right click the open18 project and choose Clean and Build.

7. Run the application

Finally! You should be able to play with the open18 seam-gen app which runs on the following url: http://localhost:8080/open18.


2 thoughts on “Seam-gen on Glassfish – part III

  1. Hi, excellent tutorial …. excuse me for my English.

    the project deploy correctly, but when I try to access any page I get this error:

    / CargosList.xhtml: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Cargos is Not mapped [select cargos from Cargos cargos]

    help me.

    • Hi, i assume you’re using a different database schema, since the open18 example database doesn’t have a Cargos table. Anyway, you should check if there’s a model class called Cargos in your project. If there isn’t, it explains your error. Maybe there is a model class called Cargo (without the trailing s). In that case, you could adjust the ejbql query in your CargosList action class.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s