【问题标题】:Entitymanager query always return an empty list using JPA on a JEE webappEntitymanager 查询总是在 JEE webapp 上使用 JPA 返回一个空列表
【发布时间】:2020-03-19 08:59:30
【问题描述】:

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

我的 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;
}

我的 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 上

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

提前致谢

【问题讨论】:

  • 我认为问题出在你的 MySQL 驱动上,试试这个:com.mysql.jdbc.Driver
  • @A.Henteti 感谢您的帮助。在persistence.xml 中?我试过了,得到这个警告“正在加载类com.mysql.jdbc.Driver'. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver'。驱动程序是通过SPI自动注册的,通常不需要手动加载驱动程序类。”仍然是一个空列表:/
  • 您的 JPQL 查询似乎不正确。您只选择了一个字段,但您正在映射到对象列表。试试select c from Employe c
  • 是的,我也试过这样,结果还是一样。我也尝试过使用其他表,我也删除并重新创建了我的整个数据库。没有任何效果

标签: maven jpa tomcat jakarta-ee entitymanager


【解决方案1】:

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

请看:PersistenceUnit vs 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

【讨论】:

  • 您的帮助。我没有使用 @PersistenceContext 因为 Tomcat 有一些限制,它不是 JEE 容器。看到这个:[stackoverflow.com/questions/11256746/… 但我很确定问题不在这里,因为数据库连接正在工作。
  • 如果你想使用Tomcat而不是我认为你需要Spring。否则我不明白谁会为你处理交易。您发送的问题是真的,是在谈论“JPA 的限制”,但是,您可以使用 Spring 实现 JPA 和事务的使用。
  • 好的,我会这样做,但它是一个小 web 应用程序,所以我想我会回到旧的 JDBC。 Ty 为您的时间
猜你喜欢
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多