繁体   English   中英

Entitymanager 查询总是在 JEE webapp 上使用 JPA 返回一个空列表

[英]Entitymanager query always return an empty list using JPA on a JEE webapp

很长一段时间以来,我一直在使用 Maven 处理我的 JEE 项目中的一个问题;

我的 entityManager.createQuery().getResultList() 总是返回一个空列表。

数据库连接没问题,我的表中有数据,并且仅使用 JDBC 就可以很好地工作。 我想我尝试了几乎所有我在互联网上看到的东西,我只是可能错过了一点......

我还尝试使用和不使用 EJB 并在 WEB-INF 目录上移动库(如果 Tomcat 无法识别它们)。

我尝试使用 JPQL 请求,如下所示,但也使用 NamedQuery 注释,但结果仍然相同。

这是我的代码(DAO):

public List<Conge> getLeaves(String username) {
    EntityManagerFactory entityManagerFactory = null;
    EntityManager entityManager = null;
    List<Conge> listLeaves = new ArrayList<Conge>();
    try {
        entityManagerFactory = Persistence.createEntityManagerFactory("ProjetJEE");
        entityManager = entityManagerFactory.createEntityManager();
        List<Employe> a = entityManager.createQuery("select c.login from Employe c").getResultList();
        System.out.println("a" + a); //always empty
    } finally {
        if ( entityManager != null ) entityManager.close();
        if ( entityManagerFactory != null ) entityManagerFactory.close();
    }
   return listLeaves;
}

我的 Employe.java(实体):

package org.gdc.models;

import java.io.Serializable;
import javax.persistence.*;
import java.util.List;


/**
 * The persistent class for the Employe database table.
 * 
 */
@Entity
@NamedQuery(name="Employe.findAll", query="SELECT e FROM Employe e")
public class Employe implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private String login;

    private String adresse;

    @Column(name="code_postal")
    private String codePostal;

    private String mail;

    private String nom;

    private String pnom;

    @Column(name="solde_congés")
    private int soldeCongés;

    private String ville;

    //bi-directional one-to-one association to Authentification
    @OneToOne(mappedBy="employe")
    private Authentification authentification;

    //bi-directional many-to-one association to Conge
    @OneToMany(mappedBy="employe")
    private List<Conge> conges;

    //bi-directional many-to-one association to Ref_Equipe
    @ManyToOne
    @JoinColumn(name="equipe")
    private Ref_Equipe refEquipe;

    //bi-directional many-to-one association to Ref_Fonction
    @ManyToOne
    @JoinColumn(name="fonction")
    private Ref_Fonction refFonction;

    //bi-directional many-to-one association to Rendez_vous
    @OneToMany(mappedBy="employe")
    private List<Rendez_vous> rendezVouses;

    public Employe() {
    }

    public String getLogin() {
        return this.login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getAdresse() {
        return this.adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    public String getCodePostal() {
        return this.codePostal;
    }

    public void setCodePostal(String codePostal) {
        this.codePostal = codePostal;
    }

    public String getMail() {
        return this.mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getNom() {
        return this.nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPnom() {
        return this.pnom;
    }

    public void setPnom(String pnom) {
        this.pnom = pnom;
    }

    public int getSoldeCongés() {
        return this.soldeCongés;
    }

    public void setSoldeCongés(int soldeCongés) {
        this.soldeCongés = soldeCongés;
    }

    public String getVille() {
        return this.ville;
    }

    public void setVille(String ville) {
        this.ville = ville;
    }

    public Authentification getAuthentification() {
        return this.authentification;
    }

    public void setAuthentification(Authentification authentification) {
        this.authentification = authentification;
    }

    public List<Conge> getConges() {
        return this.conges;
    }

    public void setConges(List<Conge> conges) {
        this.conges = conges;
    }

    public Conge addConge(Conge conge) {
        getConges().add(conge);
        conge.setEmploye(this);

        return conge;
    }

    public Conge removeConge(Conge conge) {
        getConges().remove(conge);
        conge.setEmploye(null);

        return conge;
    }

    public Ref_Equipe getRefEquipe() {
        return this.refEquipe;
    }

    public void setRefEquipe(Ref_Equipe refEquipe) {
        this.refEquipe = refEquipe;
    }

    public Ref_Fonction getRefFonction() {
        return this.refFonction;
    }

    public void setRefFonction(Ref_Fonction refFonction) {
        this.refFonction = refFonction;
    }

    public List<Rendez_vous> getRendezVouses() {
        return this.rendezVouses;
    }

    public void setRendezVouses(List<Rendez_vous> rendezVouses) {
        this.rendezVouses = rendezVouses;
    }

    public Rendez_vous addRendezVous(Rendez_vous rendezVous) {
        getRendezVouses().add(rendezVous);
        rendezVous.setEmploye(this);

        return rendezVous;
    }

    public Rendez_vous removeRendezVous(Rendez_vous rendezVous) {
        getRendezVouses().remove(rendezVous);
        rendezVous.setEmploye(null);

        return rendezVous;
    }

}

我的 pom.xml :

    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ProjetJEE</groupId>
    <artifactId>ProjetJEE</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Leave Management System</name>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>ejb-api</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>12</release>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.1</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

而我的 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="ProjetJEE"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>org.gdc.models.Authentification</class>
        <class>org.gdc.models.Conge</class>
        <class>org.gdc.models.CongePK</class>
        <class>org.gdc.models.Employe</class>
        <class>org.gdc.models.Ref_Equipe</class>
        <class>org.gdc.models.Ref_Etat_Conge</class>
        <class>org.gdc.models.Ref_Fonction</class>
        <class>org.gdc.models.Ref_Motif_Conge</class>
        <class>org.gdc.models.Ref_Type_Conge</class>
        <class>org.gdc.models.Rendez_vous</class>
        <class>org.gdc.models.Rendez_vousPK</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>

        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/ProjetJEE" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="mysql" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<!--            <property name="eclipselink.logging.level" value="FINE" />
            <property name="eclipselink.ddl-generation" value="create-tables" /> -->
        </properties>
    </persistence-unit>
</persistence>

在带有 JEE、Maven、Mysql、Tomcat9、JPA 的 Eclipse 上

如果您需要其他帮助我,请告诉我...

提前致谢

我唯一的观察是您正在从工厂创建EntityManager并且您没有注入它。 这样我认为你会在应用程序生命周期中发现问题。 您必须自己处理事务(提交、回滚等)。 我看到您正在关闭最后一个块中的连接,但我不知道您是否有交易或需要打开新交易。

请参阅: PersistenceUnit 与 PersistenceContext
https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/entity-context.html

我不知道为什么你这样做的原因,但在你的情况下,我会做这样的事情:

public abstract class GenericDAO  {    
    protected EntityManager entityManager;

    @PersistenceContext(unitName="canvasEM")
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
}

@Repository
public class EmployeDAOImpl extends GenericDAO implements EmployesDAO{
    public List<Employe> getEmployes() {
        List<Employe> list = entityManager.createQuery("select c from Employe c").getResultList();  
        return list;
    }       
}       

这是Spring ,但我想有一种方法可以直接在EJB注入EntityManager

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM