iCV
iCV

Reputation: 607

JPA NamedQuery not found

I'm trying to get a list of persons using JPA. Every time I run the code, I get "java.lang.IllegalArgumentException: NamedQuery of name: Persoon.getAllePersonen not found."

I tried changing the table name, replaced Persoon.getAllePersonen by getAllePersonen,.... I just can't seem to figure out what's causing the error

Persoon

@Entity
@Table(name = "Persoon")
@NamedQueries({
    @NamedQuery(name = "Persoon.getAllePersonen",
            query = "SELECT p FROM Persoon p"),
    @NamedQuery(name = "Persoon.findByName",
            query = "SELECT p FROM Persoon p WHERE p.achternaam = :persoonNaam OR p.voornaam = :persoonNaam")
})

public class Persoon implements Serializable {

PersoonDao

    public List<Persoon> getAlleLeden(){
            TypedQuery<Persoon> queryP = em.createNamedQuery("Persoon.getAllePersonen", Persoon.class);
                  try{ return queryP.getResultList();            
        } catch (NoResultException e){
            throw new EntityNotFoundException("Cannot find leden");
        }
    }

EDIT:

Generic Superclass DAO

public class GenericDaoJpa<T>{

    private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("TaijitanPU");

    protected static final EntityManager em = emf.createEntityManager();

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="TaijitanPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>domein.Persoon</class>
    <class>domein.Graad</class>
    <class>domein.Locatie</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost\sqlexpress:1433;databaseName=Taijitan;integratedSecurity=true;"/>
      <property name="javax.persistence.jdbc.user" value=""/>
      <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
    </properties>
  </persistence-unit>
</persistence>

Upvotes: 0

Views: 279

Answers (1)

charleskevin-so
charleskevin-so

Reputation: 26

You have to do an abstract class Generic class and override the entityManager of the parent class for each child. Check below. I used EJB Stateless for the childs.

-> PARENT DAO

public abstract class AbstractDAO<T> {
...

protected abstract EntityManager getEntityManager();

-> CHILD DAO

@PersistenceContext(unitName = "yourPersistenceUnitName")
private EntityManager em;

@Override
protected EntityManager getEntityManager() {
    return em;
}

Upvotes: 1

Related Questions