Search code examples
javahibernatejarentityseam

Entities not mapped after moving them into external jar package


In my Java/Seam/JbossAS app, I decided to externalize my Model classes (hibernate entities) and moved them into another project. The project produces model.jar, which is then used by the main app. The model.jar dependency is resolved by Ivy. Building the main app with Ant works without problems. Then I copy manually the model.jar into 'mainapp.ear/lib' directory. Afterwards I deploy the app and there are no problems (although I notice that there are is no log info about found mappings). But when I want to login, I get the exception:

javax.el.ELException: javax.ejb.EJBTransactionRolledbackException:
    org.hibernate.hql.ast.QuerySyntaxException: AppUser is not
    mapped [select u from AppUser u where u.userName = :usernamePar]

There were no code changes in the meantime, just externalizing some of the classes into a jar. Does this mean, that I need the source code of the Model classes when compiling the main app?


Solution

  • The EntityManagerFactory is built for scanning entities only from the jar that has a /META-INF/persistence.xml file into.

    In order to scan other jars you have to use <jar-file>:

    <persistence 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"
                 version="2.0">
       <persistence-unit name="manager1" transaction-type="JTA">
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
          <jta-data-source>java:/DefaultDS</jta-data-source>
          <mapping-file>ormap.xml</mapping-file>
          <jar-file>MyApp.jar</jar-file>
          <class>org.acme.Employee</class>
          <class>org.acme.Person</class>
          <class>org.acme.Address</class>
          <shared-cache-mode>ENABLE_SELECTOVE</shared-cache-mode>
          <validation-mode>CALLBACK</validation-mode>
          <properties>
             <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
          </properties>
       </persistence-unit>
    </persistence>
    

    See 2.2.1 Packaging in Hibernate doc.