Entitymanager查询始终在JEE Web应用程序上使用JPA返回空列表

问题描述 投票:0回答:1

我在Maven的JEE项目上很长时间以来一直在处理问题;

我的EntityManager.createQuery()。getResultList()始终返回一个空列表。

数据库连接正常,我的表中有数据,并且仅使用JDBC即可很好地工作。我想我尝试了几乎在互联网上看到的所有内容,只是有点遗漏了……

[我也尝试使用和不使用EJB,并且如果Tomcat无法识别库,则将库移动到WEB-INF目录。

我尝试过如下所示的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;
}

My Employe.java(Entity):

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上

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

提前感谢

maven jpa tomcat java-ee entitymanager
1个回答
0
投票

我唯一的观察结果是您正在从工厂创建EntityManager,而没有注入上下文。这样,我认为您会在应用程序生命周期中发现问题。您必须自己处理事务(提交,回滚)。我看到您正在关闭决赛中的连接,但是我不知道您是否有交易或需要打开一个新交易。请参阅:PersistenceUnit vs PersistenceContexthttps://www.logicbig.com/tutorials/java-ee-tutorial/jpa/entity-context.html

© www.soinside.com 2019 - 2024. All rights reserved.