1

I'm new to Java EE and having some trouble using named queries. We use JBoss 6 and Hibernate 3.6 (JPA 2.0). The entity is configured via annotations and not in the persistence.xml:

@Entity
@NamedQueries({
    @NamedQuery(name = "Node.findRootNodes", query = "SELECT n FROM Node n WHERE n.parent IS NULL OR n.parent = 0"),
    ...
})
public class Node implements Serializable { ... }

When deploying the jar containing the entity, the JBoss-log tells us, that the named query is bound:

[org.hibernate.cfg.annotations.QueryBinder] Binding Named query: Node.findRootNodes => SELECT n FROM Node n WHERE n.parent IS NULL OR n.parent = 0

Unfortunately, when using the named query, we always run into an IllegalArgumentException:

[com.NodeTest] null: javax.ejb.EJBException: java.lang.IllegalArgumentException: Named query not found: Node.findRootNodes

The stateless bean using the named-query is in the same jar as the Node-Entity. The Application calling the stateless bean is in a different war file deployed.

Does anybody has an idea what I do wrong? Thank you for your Answer.

Here is the persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
    <persistence-unit name="testPU" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider> 
        <jta-data-source>java:/main</jta-data-source>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
        </properties>
    </persistence-unit>
</persistence>

2 Answers2

4

What to check:

  • if your EntityManager was created by the EntityManagerFactory representing the proper persistence unit
  • whether your Entity is actually handled by the given persistence unit. Try loading an entity with a given id.
  • whether other named queries on other entities are found

Update 1: It seems your entity is not loaded by the persistence unit. Check your persistence.xml for <exclude-unlisted-classes> and remove it. Hibernate should support automatic detection of entities, but just to verify - list your class in persistence.xml

Update 2: Make sure your @Entity annotation is actually javax.persistence.Entity, and not org.hibernate....

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • The EntityManager is injected using `@PersistencUnit`. There is only one persistence unit defined in __persistence.xml__. Loading an entity with a given id fails with an `IllegalArgumentException: Unknown entity`. Hm... so the entity is actually not handled by the persistence unit? – Flurin Juvalta Jun 09 '11 at 15:43
  • yes. you need to inject it using @PersistenceContext btw. for the rest - check updated. – Bozho Jun 09 '11 at 15:47
  • Ups sorry, of course I injected with `@PersistenceContext`, I tried your advice to list the entity classes in the persistence.xml, but still `Unknown entity`. btw. I updated the post with my persistence.xml – Flurin Juvalta Jun 09 '11 at 16:02
  • well, you are not getting the right persistence context then.. but I don't know why. – Bozho Jun 09 '11 at 16:03
  • Can there be a Problem calling the stateless bean where the EntityManager is injected from an Application (Servlet) deployed in a different war file? The persistence.xml is placed in the jar containing the stateless bean and the entities. – Flurin Juvalta Jun 09 '11 at 16:06
  • did you check the `@Entity` annotation? – Bozho Jun 09 '11 at 16:07
  • Yes, it is `javax.persistence.Entity` – Flurin Juvalta Jun 09 '11 at 16:07
1

I could solve this issue by moving the persistence.xml inside the war that contains the application instead of the jar that contains the entities and the bean. Additionally I had to add the following line to my persistence.xml:

<jar-file>Entities.jar</jar-file>

But now I have to place the persistence.xml in any application that uses my Entities.jar.