0

I have the following structure of a web application (war) running on JBoss EAP 6.1 with JPA 2 and Hibernate 4.2:

  • Entity.jar (from other internal project)
  • Application.war (containing the Entity.jar in WEB-INF/lib, included as maven dependency, not as maven module)

In my web application project (Application.war) I have an EJB containing an EntityManager. In the EJB I would like to create a JQL-Query (em.createQuery) for an Entity from Entity.jar.

If I call the method of the EJB containing the query, I got the error:

Caused by: javax.ejb.EJBException: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [SELECT u FROM User as u WHERE u.name = :name]

My entity User from Entity.jar looks like:

@Entity
@Table(name = "PL1_USER")
public class User implements Serializable {
...

EJB from Application.war:

@Stateless
public class UserService {

@PersistenceContext(unitName = "my-unit")
private EntityManager entityManager;

public User getUser(String username) {
    TypedQuery<User> query = entityManager.createQuery("SELECT u FROM User as u WHERE u.name = :name", User.class);
    query.setParameter("name", username);
    return query.getSingleResult();
}
}

persistence.xml from Application.war (WEB-INF/classes/META-INF):

<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="my-unit" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:jboss/datasource/MY-DS</jta-data-source>

    <jar-file>Entity.jar</jar-file>

    <properties>
        <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
        <property name="hibernate.archive.autodetection" value="class, hbm" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.use_sql_comments" value="false" />
        <property name="hibernate.jdbc.batch_size" value="30" />
        <property name="hibernate.jdbc.fetch_size" value="100" />
        <property name="hibernate.max_fetch_depth" value="0" />
        <property name="hibernate.order_updates" value="true" />
        <property name="hibernate.use_identifier_rollback" value="true" />

        <!-- JDBC Driver returns row count for batch statements -->
        <property name="hibernate.jdbc.batch_versioned_data" value="true" />

        <property name="hibernate.dialect" value="org.hibernate.dialect.DB2400Dialect" />

    </properties>
</persistence-unit>

Structure of Application.war:

- Application.war
    - META-INF
        - MANIFEST.MF (containing Entity.jar in Class-Path)
    - resources
    - WEB-INF
        - com
            - example
                - UserService.class
        - classes
            - META-INF
                - persistence.xml
        - lib
            - Entity.jar
    - index.xhtml

If you need more information/details please let me know.

I already searched on google, but nothing that I found worked for me.

I'm very happy if anyone can help me.

mbulau
  • 359
  • 1
  • 7
  • 19

2 Answers2

0

The book Pro JPA2 notes the following about including entities from JAR files:

When listing a JAR in a jar-file element, it must be listed relative to the parent of the JAR file in which the META-INF/persistence.xml file is located. This matches what you would put in the classpath entry in the manifest. For example, assume the enterprise archive (EAR), that we will call emp.ear, is structured as shown in Listing 13-5.

Listing 13-5. Entities in an External JAR

  • emp.ear
    • emp-ejb.jar
      • META-INF/persistence.xml
    • lib/emp-classes.jar
      • examples/model/Employee.class

The contents of the persistence.xml file should be as shown in Listing 13-6, with the jar-file element containing “lib/emp-classes.jar” to reference the emp-classes.jar in the lib directory in the EAR file. This would cause the provider to add the annotated classes it found in emp-classes.jar (Employee.class) to the persistence unit, and because the jar is in the lib directory of the EAR, it would automatically be on the application classpath.

Listing 13-6. Contents of persistence.xml

<persistence-unit name="EmployeeService">
<jta-data-source>java:app/jdbc/EmployeeDS</jta-data-source>
<jar-file>lib/emp-classes.jar</jar-file>
</persistence-unit>
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
  • For now, I don't have an ear (it's only a war), but I already tried to setting the the jar-file value to "lib/Entity.jar" with no success. Is an ear file the only way of running it? – mbulau Mar 04 '15 at 11:38
  • You will need to update the question with the structure of your deployed application. – Alan Hay Mar 04 '15 at 11:40
  • I've added the structure of the Application.war to the question. – mbulau Mar 04 '15 at 12:01
  • Seee here: http://stackoverflow.com/questions/4433341/what-is-the-right-path-to-refer-a-jar-file-in-jpa-persistence-xml-in-a-web-app – Alan Hay Mar 04 '15 at 13:52
0

The problem seems to be in reference to the jboss-deployment-structure.xml. The Entity.jar is also used for the login mechanism (JAAS) for the Application.war and therefor the same Entity.jar is available as EAP module and included in the jboss-deployment-structure.xml of the Application.war.

Maybe the JBoss EAP ignores the Entity.jar in the lib directory of the Application.war because it's already declaration in jboss-deployment-structure.xml and included via EAP module.

mbulau
  • 359
  • 1
  • 7
  • 19