14 Replies Latest reply on Jul 14, 2009 7:10 PM by Chris Dunphy

    Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?

    Chris Dunphy Newbie

      I started a really simple seam-gen project with GlassFish 2.1 and MySQL using seam 2.1.2.CR2 on an Ubuntu workstation.  There is one Entity class:

      package rcd.hello.model;
      import javax.persistence.*;
      public class User {
           private long id;
           private String username;
           private String password;
           public long getId() {
                return id;
           public void setId(long id) {
                this.id = id;
           public String getUsername() {
                return username;
           public void setUsername(String username) {
                this.username = username;
           public String getPassword() {
                return password;
           public void setPassword(String password) {
                this.password = password;

      I ran seam setup, seam create-project.  Here is my seam-gen.properties file:

      #Generated by seam setup
      #Thu May 28 15:52:23 MDT 2009

      I ran seam generate-ui to create a view based on the one entity class that belongs to the project.  So far so good.  I ran ant gf-prepare, and I made the following changes to the seam-gen project (as per the glassfish-readme.txt file):


         <!-- Add entries for each EJB session bean which is also a Seam component (not required on JBoss AS) -->
            <!-- The relative reference doesn't work on GlassFish. Instead, set the <persistence-unit-name> to "hello",
                 package persistence.xml in the WAR, and add a <jar-file> element in persistence.xml with value "../../hello.jar". -->


      <?xml version="1.0" encoding="UTF-8"?>
      <!-- Persistence deployment descriptor for dev profile -->
      <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
         <persistence-unit name="hello">
            <!-- The <jar-file> element is necessary if you put the persistence.xml in the WAR and the classes in the JAR -->
               <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
               <property name="hibernate.hbm2ddl.auto" value="update"/>
               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.format_sql" value="true"/>
               <property name="hibernate.default_catalog" value="hello"/>
               <property name="jboss.entity.manager.factory.jndi.name" value="java:/helloEntityManagerFactory"/>

      Ok, so far its great.  I can run gf-explode and the project is deployed to GlassFish no problem.  I can use the default authenticator session component to login if I supply the admin username and the blank password.

      Here is where the problems start.  First I add a username/password to the database.  Then if I change AuthenticatorBean to do this:

      package rcd.hello.action;
      import javax.ejb.Stateless;
      public class AuthenticatorBean implements Authenticator {
           private Log log;
           EntityManager entityManager;
           Identity identity;
           Credentials credentials;
           public boolean authenticate() {
                log.info("authenticating {0}", credentials.getUsername());
                User user = (User) entityManager.createQuery(
                          "select u from User u where username = :username")
                          .setParameter("username", credentials.getUsername())
                if (user == null) {
                     log.error("no matching user found in the database");
                     return false;
                if (user.getPassword().equals(credentials.getPassword())) {
                     log.info("Authentication successful for user {0}", credentials
                     return true;
                log.info("Invalid password");
                return false;

      It doesn't work.  I get the following exception:

      javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean; neste
      d exception is: java.lang.IllegalArgumentException: EntityManagerFactory not fou
      nd in JNDI : java:comp/env/hello/pu
      java.lang.IllegalArgumentException: EntityManagerFactory not found in JNDI : jav
              at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManager
              at org.jboss.seam.persistence.ManagedPersistenceContext.initEntityManage
              at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManager
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.

      This is a dead-simple EAR based project based clean right off of Seam gen.  I can't seem to get the seam managed persistence to work with EJB/seam session bean components on glassfish, not off the project structure created by seam-gen.

        • 1. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
          Thai Dang Vu Apprentice

          I got the exactly same errors with Seam 2.1.1.GA and Glassfish 2.1 when I tried to inject an entityManager to a stateful session bean. Then I had to use @PersistenceContext (not @In) for the entityManager.

          Then the question is: what features will I lose when using @PersistenceContext not @In?

          • 2. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
            Arshad Mahmood Newbie


            You will lose manual flush mode. This is a really useful feature when developing with conversation based semantics.

            I came across the same issue. It seems to affect mostly the @Remove method. When that method is called the binding under java:comp/env dispears !

            I was told that I should explicitly specify the JNDI binding in ejb-jar.xml or via an annotation, as under JEE5 that is the only guaranteed way to access anything in JNDI within an EJB. I couldn't see how to do this (nor did I want to do it, given that I have around 50 beans).


            • 3. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
              Chris Dunphy Newbie

              So I tried to change:

              EntityManager entityManager


              EntityManager entityManager;

              When you make that change, the app fails to deploy on GlassFish:

              [exec] CLI171 Command deploy failed : Deploying application in domain failed; Could not resolve a persistence unit corresponding to the persistence-context-ref-name [rcd.hello.action.AuthenticatorBean/entityManager] in the scope of the module called []. Please verify your application.

              I have until tomorrow to figure out a workaround for this; or I'll have to abandon Seam for this upcoming project :-(

              • 4. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
                Arshad Mahmood Newbie

                Have you tried adding something like the following in your web.xml


                I haven't tried this, but one of the Glassfish guys mentioned that this might solve the problem.
                • 5. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
                  Chris Dunphy Newbie

                  Thanks for trying to help,

                  I tried that and it didn't make any difference.  I also tried a bunch of other things, such as replacing:

                  <persistence:managed-persistence-context name="entityManager" auto-create="true"


                            name="entityManagerFactory" persistence-unit-name="hello" />
                            name="entityManager" auto-create="true" entity-manager-factory="#{entityManagerFactory}" />

                  However, that also failed to work...

                  Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionM
                          at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:329)
                          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
                          at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
                          at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:730)
                          ... 97 more

                  Its surprisingly frustrating.  I tried following the instructions at:

                  But I am not sure if they apply to Seam 2.1.2, but in any event they didn't work.  I am running out of things to try.  Seam looks fantastic and I really want to use it, but we have our Glassfish instances here at work, so I need it to work with GlassFish, and much of our code leverages EJB3, so if I can't get basic Seam features to work on an EAR project running on GlassFish I am dead in the water.

                  Considering GlassFish support is a feature for 2.1.2 I am really surprised that it is this hard to get working.  If I am missing something obvious, and I need to be awarded with the Moronic developer of the month award I'd love it if someone could point it out to me and show me what I am missing to get this working.  I am not a Java EE guru, I've written a few apps that use Struts2 on the front-end with EJB3/JPA in the back end.  Usually, I just stick the persistence.xml file in the EJB-JAR under the META-INF folder and it just works.

                  I posted here because the Seam website suggests getting consensus on a bug before posting to JIRA.  I am hoping that someone can look at this very simple use case and figure out why this doesn't work on GlassFish.

                  Btw, resetting back to using @puJdniName@ as seam-gen set it up, if I do gf-deploy instead of gf-explode, the app fails to deploy:

                  [exec] CLI171 Command deploy failed : Deploying application in domain failed; Could not resolve a persistence unit corresponding to the persistence-unit-ref-name [hello/pu] in scope of the module called []. Please verify your application.

                  It only deploys at all if I use the gf-explode.  It really looks like support for GlassFish is broken for EAR projects on Seam 2.1.2.CR2.  Someone, please prove me wrong. Call me a moron, I can take it and move on with this project and dazzle my manager with what Seam can do.

                  // Chris

                  • 6. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
                    Arshad Mahmood Newbie


                    Seam with Glassfish definitely works, look at the Seam Booking example in Seam example under JEE. But that application uses @PersistenceContent rather than @In to inject an entity manager.

                    I suggest you look at the generated EAR from seam booking and your own EAR to see what the difference is.

                    I still think it is wrong to have the  persistence.xml under WEB-INF/META-INF. Glassfish recommends that you create a jar with persistence.xml and put in the EAR lib folder (I have a jar which JUST contains the persistence.xml, a bit overkill but I can't any other way to do it) and that works fine. BUT, I don't use EJB because I can't get @In to work properly.

                    If you post a directory listing of your EAR, the maybe I can tell you what is wrong.


                    • 7. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
                      Chris Dunphy Newbie

                      Hey Robert,

                      The problem is that I can't use seam-gen then, nor can I use seam managed persistence.  None of the examples I saw in the seam distribution closely resemble what I get from seam-gen, which I want to use in order to generate CRUD views of my entities.  It saves so much work. 

                      How do I kick it off with seam-gen and then fix it so it works?  I tried moving the persistence.xml file to the EJB-JAR under META-INF (and removing the jar-file tag from it, but that breaks everything as well.

                      Hers is what the EAR looks like when I run the gf-deploy target:

                      Megaparsec:hello cdunphy$ ls
                      META-INF     hello.jar     hello.war     jboss-seam.jar     lib
                      Megaparsec:hello cdunphy$ cd META-INF/
                      Megaparsec:META-INF cdunphy$ ls
                      MANIFEST.MF     application.xml     jboss-app.xml
                      Megaparsec:META-INF cdunphy$ cat application.xml 
                      <?xml version="1.0" encoding="UTF-8"?>
                      <application xmlns="http://java.sun.com/xml/ns/javaee" 
                                   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd"
                         <!-- Seam and EL -->
                      Megaparsec:META-INF cdunphy$ 

                      The listings at the top of this thread show what seam-gen gives me.  Here is the glassfish-readme.txt file that comes with seam.  I followed all of its directions precisely:

                      First, you must prepare GlassFish for a seam-gen project (i.e., deploy Hibernate as a JPA provider)
                        ant gf-prepare
                      Next, you need to start GlassFish:
                        ant gf-start
                      Finally, you can deploy the project:
                        ant gf-explode
                      GlassFish deployment works out of the box for WAR projects. To deploy an EAR project, make the following changes:
                        1. Uncomment the <ejb-local-ref> entries in resources/WEB-INF/web.xml (include additional entries as necessary)
                        2. Strip the contents up to and including the # in the element <persistence-unit-name> in resources/WEB-INF/web.xml
                        3. Uncomment the <jar-file> element in resources/META-INF/persistence-dev.xml & resources/META-INF/persistence-prod.xml
                      If you plan to use the default Derby datasource in GlassFish, named jdbc/__default, then uncomment the following property
                      in the build.properties file at the root of the project to prevent the gf-deploy-datasource target from executing:
                      When switching back and forth between a JBoss AS deployment and a GlassFish deployment, be sure to clean the project:
                        ant clean

                      Like I said, it I use explode it deploys, and if you click the UserList view, that works.  It is when you try to use a session bean based component that the injection of the EntityManager fails.

                      I am hoping someone can try a similar basic example with seam-gen using Seam 2.1.2.CR2 with GlassFish and see what I am talking about.

                      // Chris

                      • 8. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
                        Chris Dunphy Newbie


                        Okay, so yeah, the way seam-gen sets up the project doesn't work for EAR projects on GlassFish.  I had to take the following steps to fix it (starting from the point where you run seam-gen and follow the instructions in glassfish-readme.txt):

                        1. Disable the ant target that moves the persistence.xml file to /WEB-INF/classes/META-INF:

                        <target name="gf-cleanup-ear" if="project.ear">
                                  <move todir="${war.dir}/WEB-INF/classes">
                                       <fileset dir="${jar.dir}">
                                            <include name="META-INF/orm.xml" if="project.ear" />
                                            <include name="META-INF/persistence.xml" if="project.ear" />

                        2. Comment out the persistence-unit-ref entry in web.xml

                        3. Don't try to lookup the seam managed context using @puJndiName@ in components.xml... instead use this:

                                  name="entityManagerFactory" persistence-unit-name="hello" />
                                  name="entityManager" auto-create="true" entity-manager-factory="#{entityManagerFactory}" 

                        4. Add ejb-transactions as well to components.xml (you need to add stuff to the namespace headers, see link at the end):

                        <tx:ejb-transaction />

                        5. Comment out the jboss.entity.manager.factory.jndi.name property in the persistence-dev.xml file.

                        6. Add the following property to persistence-dev.xml:

                        <property name="hibernate.transaction.manager_lookup_class"
                             value="org.hibernate.transaction.SunONETransactionManagerLookup" />

                        7. Add an EJB ref for org.jboss.seam.transaction.LocalEjbSynchronizations to web.xml:

                                  <local-home />

                        Clearly, some adjustments need to be made for seam-gen for GlassFish EAR projects.  The instructions by Dan Allen at:
                        are what actually work.

                        I was able to test this both with the gf-deploy and the gf-explode targets.

                        Could somebody confirm this, I think a JIRA issue would be appropriate so that this can be fixed.

                        But first I shall proceed to do the Balky Bartokomous dance of joy!!!

                        // Chris

                        • 9. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
                          Arshad Mahmood Newbie


                          I tried to follow your instructions but I am finding that the resulting entity manager doesn't load the persistence unit properly (i.e. when using the entity manager I get resources not mapped exceptions).

                          If yours is working properly can you answer th follwing questions:-

                          1. Where is your persistence.xml and what does it contain?
                          2. Where are your entities packaged ?
                          3. Do you still have persistence-unit-ref in the web.xml?


                          • 10. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
                            Chris Dunphy Newbie

                            Hello Robert,

                            1. Because I disable the gf-cleanup-ear target in glassfish-build.xml, the persistence.xml file will be in the EJB-JAR under META-INF.  I may have forgot to mention that you don't want the jar-file tag in the persistence.xml file either.. here is what mine looks like:

                            <?xml version="1.0" encoding="UTF-8"?>
                                 <!-- Persistence deployment descriptor for dev profile -->
                            <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
                                 <persistence-unit name="hello">
                                           <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
                                           <property name="hibernate.hbm2ddl.auto" value="update" />
                                           <property name="hibernate.show_sql" value="true" />
                                           <property name="hibernate.format_sql" value="true" />
                                           <property name="hibernate.default_catalog" value="hello" />
                                           <property name="hibernate.transaction.manager_lookup_class"
                                                value="org.hibernate.transaction.SunONETransactionManagerLookup" />

                            2. The entities are packaged in the EJB-JAR as well... under the src/main directory.

                            3. No, step 2 above does outline that I blew away the persistence-unit-ref in the web.xml.

                            I tried these steps on a couple of instances now, and on my bigger project and the steps do work for me with GlassFish.  So I am able to use seam-gen to kick off the CRUD UI and then tweak it for GlassFish.  If anyone is able to confirm this, perhaps a JIRA issue would be helpful so that the new GlassFish support that is built into seam-gen can be fixed to work better for EAR projects?

                            • 11. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
                              Arshad Mahmood Newbie

                              Hi Chris,

                              Thanks for the clarification.

                              The only difference between my setup and yours is that my persistence.xml is packaged in a JAR and put in the lib folder of the EAR and the persistence.xml contains two jar-file elements (I can't avoid this as the entities have to be packaged this way because of the project structure)..

                              With this setup I get no errors but the persistence unit is not loaded. I will investigate further, I wonder if the seam builtin persistence unit loader doesn't support jar-file or something.

                              I am only playing with this because it would be nice to get full JEE working rather than using basic seam components. I can get it seam working by using persistence-unit-ref and the entity manager factory that inserts into JNDI, but as mentioned that causes problem with EJB beans.

                              It's great that you got it working.


                              • 12. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
                                Dan Allen Master

                                Sigh. I realize that this is a very difficult problem. Trust me, it took me 6 months to figure out how to get everything set up right.

                                Let me try to explain something that is extremely important to understand, starting with the component configuration below.

                                 name="entityManagerFactory" persistence-unit-name="hello" />
                                  name="entityManager" auto-create="true" entity-manager-factory="#{entityManagerFactory}"/>

                                This is a Seam-managed persistence unit bootstrap. Seam is leveraging the Java SE bootstrap capabilities of JPA to load the persistence unit. It is absolutely the easiest way to bootstrap the persistence unit. There are virtually no packaging restrictions when using this approach.

                                However, when Seam bootstraps the persistence unit, you cannot use @PersistenceContext. Once you go Seam, you can't go back to using the Java EE resource injections. That's because the container is not aware of the Java SE bootstrap or the resulting EntityManagerFactory.

                                Of course, you can still use @In to inject the Seam-managed persistence manager and for local EJBs, I would recommend using that configuration.

                                The point of the seam-gen GlassFish support is to demonstrate how to get the container to bootstrap the persistence unit. It is a huge pain in the ass because GlassFish imposes all these restrictions on packaging. Those restrictions are:

                                • Entity classes and persistence.xml should be packaged in their own JAR

                                • If they are not, then you must do a very bizarre relative reference in your persistence unit descriptor (or resource reference if GlassFish would work right)

                                Well, seam-gen only creates two archives, a WAR and an EJB-JAR. Therefore, you have to put the persistence.xml in the WAR and then use a relative reference back to the EJB-JAR defined in the persistence unit descriptor:

                                <persistence-unit name="hello">

                                Depending the version of GlassFish, this bit of magic may not work (believe me, it was hard enough to figure out that it actually works on some version of GlassFish). But I'm telling you that I use this in both GlassFish V2 and V3 and it works fine for me.

                                • 13. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
                                  Dan Allen Master

                                  When the application is starting, make sure you see the persistence unit bootstrap before Seam starts loading. That is an indication that the container is doing its job. Make sure you have the Hibernate libraries in the GlassFish domain if the persistence unit is not bootstrapping.

                                  • 14. Re: Bug in seam-gen (Seam 2.1.2.CR2) for GlassFish EAR Projects?
                                    Chris Dunphy Newbie

                                    Sorry if it took a while to reply, I just got back from vacation.

                                    I have never had a problem using entity classes in the EJB-JAR on GlassFish.  In fact, the changes I make to the seam-gen setup puts them in there and it works.  When I've used entity classes in more vanilla EJB3 projects I've never had to put the entity classes in a separate JAR file.  Are you certain that this is a requirement?  Wouldn't it just be simpler to configure seam-gen to put the entity classes in the EJB JAR file?

                                    However, I appreciate that this is hard.  I have workarounds that work for now, so I am very happy.  Thanks Dan for working so hard to help with GlassFish support!  I am really excited about Seam and it seems to be working out for my new project.