4 Replies Latest reply on Mar 29, 2011 1:58 PM by alesj

    Hibernate entity scanning with JBoss6 and Seam

    grodd

           We’re currently migrating an application from JBoss 4.2.3 toJBoss 6.0.0 and we’re having issues with the initial hibernate entity scanningwith Seam 2.2.0.  When the application isdeploying the logs show the entity classes being bound to their tablenames.  Once Seam kicks in however,another EntityManagerFactory is created, and the classes are scanned again, butthis time the entities are not found.  Ifthe entities are manually added to the persistence.xml file, then theapplication deploys correctly. 

       

      Should theapplication use the EntityManagerFactory created by JBoss? Or is there a way to configure Seam so that it can properly scan the classes?

        • 1. Hibernate entity scanning with JBoss6 and Seam
          genman

          Seam should probably use the JBoss one.

           

          I'm guessing some sort of Seam example out there (maybe for JBoss 5?) would be a good way to start looking for how to configure this.

          • 2. Hibernate entity scanning with JBoss6 and Seam
            grodd

            Thanks for the response Elias. 

             

            Based on your suggestion, I started looking into using the EntityManager created by JBoss.  As it turns out, due to some recent changes to our project, the hibernate entity manager wasn't even being created by JBoss anymore.  Based on the changes that were made, and the things they fixed, we figured it best to just let Seam handle the entity manager.  So I kept digging and I managed to find a solution that seems to work well for us which I'll detail out now just incase someone is in the same situation.

             

            The root of the problem was that when seam initilized the EntityMangerFactory, the scans for the classes annotated by @Entity was performed by org.hibernate.ejb.packaging.NativeScanner, which doesn't work under JBoss.  What needed to run was org.jboss.scanning.hibernate.ScannerImpl which had support for VFS.

             

            In persistence.xml, there is a property to set the resouce scanner hibernate.ejb.resource_scanner that can be used to specify the scanner used to find the entity classes.  However it's not a simple matter of just setting it to ScannerImpl because the constuctor of ScannerImpl requires a org.jboss.deployers.structure.spi.DeploymentUnit.  So one would have to create their own implementation of org.hibernate.ejb.packaging.Scanner, and in it get the current deployment unit and create a delegate that is ScannerImpl.  To get the DeplotmentUnit my code looks something like this...

             

            public JBossScannerImpl() {

                ClassLoader cl = this.getClass().getClassLoader();

                Module module = ClassLoading.getModuleForClassLoader(cl);

                if (module instanceof AbstractDeploymentClassLoaderPolicyModule) {

                  AbstractDeploymentClassLoaderPolicyModule deploymentModule =

                      (AbstractDeploymentClassLoaderPolicyModule) module;

                  DeploymentUnit unit = deploymentModule.getDeploymentUnit();

                  _delegate = new ScannerImpl(unit);

                }

              }

             

            Then, to fill out the methods required by implementing Scanner one can delegate the responsibility off to ScannerImpl.  Finally all that has to be done is to refer to the new scanner implementation in persistence.xml

             

            <persistence-unit name="SomePersistenceUnit" transaction-type="RESOURCE_LOCAL">

                <provider>org.hibernate.ejb.HibernatePersistence</provider>

                <properties>

                  <property name="hibernate.ejb.resource_scanner" value="your.scanner.Implemtation" />

                </properties>

              </persistence-unit>

             

            And that shoud do the trick, I don't know if this is the best solution, and I think that if you have access to the EntityManager created by JBoss, that would be the way to go.  But this seems to work for us.

            • 3. Hibernate entity scanning with JBoss6 and Seam
              ofd

              Thx, that post really saved my day .

               

              I finally got rid of the damn "java.lang.IllegalArgumentException: Named query not found:" when migrating our ear from jboss4 to jboss6!

              • 4. Hibernate entity scanning with JBoss6 and Seam
                alesj

                Who creates EntityManagerFactory in this case?

                Seam? But doesn't use JBossAS EMF creation?

                 

                JBossAS' EMF should already use ScannerImpl.

                If not, then there is some bug in MC Scanning lib.

                 

                And that shoud do the trick, I don't know if this is the best solution

                What about if instead of creating new ScannerImpl,

                you look into DeploymentUnit if the Scanner attachment is already there?

                 

                e.g. _delegate = unit.getAttachment(Scanner.class);