找不到JPA NamedQuery

时间:2019-04-24 10:12:11

标签: sql-server jpa netbeans

我正在尝试获取使用JPA的人员列表。每次运行代码时,都会得到“ java.lang.IllegalArgumentException:名称为NamedQuery的名称:未找到Persoon.getAllePersonen”。

我尝试更改表名称,用getAllePersonen代替Persoon.getAllePersonen,.....我似乎无法弄清楚是什么原因导致了错误

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");
        }
    }

编辑:

通用超类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>

1 个答案:

答案 0 :(得分:1)

您必须做一个抽象类泛型类,并为每个孩子覆盖父类的 entityManager 。检查下面。我为孩子使用了 EJB Stateless

->父母DAO

<div class="profile-picture big-profile-picture" *ngIf="displayImage">
    <img src="{{displayImage}}">
</div>

->儿童DAO

public abstract class AbstractDAO<T> {
...

protected abstract EntityManager getEntityManager();