I have successfully used javax.persistence.NamedQuery
in combination with JPA2. The named queries are defined at the top of the entity class files, and are used from Stateless EJBs (entity facade).
Now I had to extract the entity class files into a separate Jar file (so we can use them from a Google Web Toolkit project). Obviously I still incude the jar, but now the facade bean does not find the query anymore:
java.lang.IllegalArgumentException: NamedQuery of name: Store.findByExternalId not found.
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getDatabaseQueryInternal(EJBQueryImpl.java:576)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1004)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createNamedQuery(EntityManagerWrapper.java:533)
at com.skalio.bonusapp.server.StoreFacade.getByExternalId(StoreFacade.java:43)
...
What is the problem here? Can I not define NamedQueries in an external Jar?
I just found this link, suggesting to put the NamedQueries in XML files, not as annotations in the entity files. This could be an idea to solve my problem, but doesn't answer my question... ;)
The solution is to specify the classes containing JPA Entities in the persistence.xml file:
<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="bonusAppServerPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/someDB</jta-data-source>
<class>com.skalio.bonusapp.core.Store</class>
</persistence-unit>
</persistence>
Background is that JPA needs to be told where to scan for annotations. The other option is to use the <jar-file></jar-file>
node.
来源:https://stackoverflow.com/questions/6170527/namedquery-illegalargumentexception-query-not-found-after-externalizing-entit