为什么我的实体管理器返回空的 ResultLists?

票价

我的 Web 应用程序遇到了相当大的困境。

对于我的学校项目,我们必须从经典的 JDBC 集成迁移到 JPA 集成。至于我自己,我决定使用 Hibernate JPA Framework。我试过在 SessionBean 中的 main 中,它在那里工作。但是每当我将它集成到 Web Servlet 中时,我注意到它返回了空列表。我试过用System.out.println().

不管怎么说,我觉得这个问题可能是我的persistence.xml,更具体地缺乏的<jta-data-source>something here</jta-data-source>在里面。

这是我的persistence.xml,也许您可​​以看到我遇到的问题:

<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="PERSISTENCE" transaction-type="JTA">
    <description>Hibernate JPA Configuration Example</description>
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>JavaBeans.Employee</class>
    <class>JavaBeans.User</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/JEEPRJ?serverTimezone=Europe/Paris"/>
      <property name="javax.persistence.jdbc.user" value="jee"/>
      <property name="javax.persistence.jdbc.password" value="jee"/>
      <property name="javax.persistence.jdbc.serverTimezone" value="Europe/Paris"/>
      <property name="javax.persistence.jdbc.useSSL" value="false"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
      <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
    </properties>
  </persistence-unit>
</persistence>

这是我的 Employee 会话 Bean:

package SessionBeans;

import JavaBeans.Employee;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import javax.persistence.Query;

/**
 *
 */
@Stateless
public class EmployeeSB {

    @PersistenceContext(name="PERSISTENCE")
    EntityManager em;

    public List<Employee> getAllEmployees(){

        String query = "SELECT e FROM Employee e ";
        Query q  = em.createQuery(query);
        List<Employee> employees = q.getResultList();
        if(employees!=null){
            System.out.println("it's not null list size : " + q.getResultList().size());
            for(Employee emp:employees){
                System.out.println("id  : " + emp.getId());
            }
            return employees;
        }
        System.out.println("it's null");
        return employees;
    }

还有我的 Employee 类:

@Entity
@Table(name = "EMPLOYE")
public class Employee implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer id;
    @Size(max = 255)
    @Column(name = "NOM")
    public String nom;
    @Size(max = 255)
    @Column(name = "PRENOM")
    public String prenom;
    @Size(max = 255)
    @Column(name = "TELDOMICILE")
    public String telDomicile;
    @Size(max = 255)
    @Column(name = "TELPORTABLE")
    public String telPortable;
    @Size(max = 255)
    @Column(name = "TELPRO")
    public String telPro;
    @Size(max = 255)
    @Column(name = "ADRESSE")
    public String adresse;
    @Size(max = 255)
    @Column(name = "CODEPOSTAL")
    public String codePostal;
    @Size(max = 255)
    @Column(name = "VILLE")
    public String ville;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Size(max = 255)
    @Column(name = "EMAIL")
    public String email;


    public Employee() {

    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNom() {
        return nom;
    }

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

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public String getTelDomicile() {
        return telDomicile;
    }

    public void setTelDomicile(String telDomicile) {
        this.telDomicile = telDomicile;
    }

    public String getTelPortable() {
        return telPortable;
    }

    public void setTelPortable(String telPortable) {
        this.telPortable = telPortable;
    }

    public String getTelPro() {
        return telPro;
    }

    public void setTelPro(String telPro) {
        this.telPro = telPro;
    }

    public String getAdresse() {
        return adresse;
    }

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

    public String getCodePostal() {
        return codePostal;
    }

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

    public String getVille() {
        return ville;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }





    public Employee(String nom, String prenom, String telDomicile, String telPortable, String telPro, String adresse, String codePostal, String ville, String email) {
        this.nom = nom;
        this.prenom = prenom;
        this.telDomicile = telDomicile;
        this.telPortable = telPortable;
        this.telPro = telPro;
        this.adresse = adresse;
        this.codePostal = codePostal;
        this.ville = ville;
        this.email = email;
    }



    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Employee)) {
            return false;
        }
        Employee other = (Employee) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "JavaBeans.Employee[ id=" + id + " ]";
    }
}

(别介意法语哈哈)。

无论如何,如果有人看到实体管理器返回空列表的原因,那将对我有很大帮助,并使我了解我在哪里犯了愚蠢的错误。

非常感谢你们,祝你们有美好的一天

票价。

票价

我设法找到了问题所在。系好安全带,因为这将是一段漫长的旅程。

在我的persistence.xml我从来没有定义 a<jta-data-source></jta-data-source>因为我不明白它的目标以及为什么我们需要使用它。

从那以后,我积累了大量的知识和智慧(不是真的,基本上是试错哲学)。

所以。说够了。直接回答。

正如我之前所说,<jta-data-source></jta-data-source>没有定义persistence.xml代替一个巨大的<properties></properties>部分。

Netbeans IDE 在您目录的 WEB-INF 文件夹中生成一个文件。它被称为glassfish-resources.xml我们persistence.xml有点受它约束。在资源文件中,我们定义了所有 JDBC 连接属性。我们通过 JNDI 在池中命名它。

因此我们新的 `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="m1se_appv2_war_1.0PU" transaction-type="JTA">
    <jta-data-source>java:app/DBJEE</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

还有(著名的)glassfish-resources.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
    <jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_JEEPRJ_jeePool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
        <property name="serverName" value="localhost"/>
        <property name="portNumber" value="3306"/>
        <property name="databaseName" value="JEEPRJ"/>
        <property name="User" value="jee"/>
        <property name="Password" value="***"/>
        <property name="URL" value="jdbc:mysql://localhost:3306/JEEPRJ?useSSL=false&amp;serverTimezone=Europe/Paris"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="java:app/DBJEE" object-type="user" pool-name="mysql_JEEPRJ_jeePool"/>
</resources>

顺便说一句,如果您使用的是 5.7 以上的 MySQL 版本,如果您不打算使用它,请绝对将 URL 中的 useSSL 属性定义为 false。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring,Hibernate,JPA-为什么我不使用实体管理器,为什么casacdeType.Persist仅与实体管理器一起使用

为什么实体管理器为每个实体刷新相同的日期?

我的变量为什么返回空?

为什么 mongodb 从变更流返回空实体?

为什么我的Windows资源管理器不再自我刷新?

事务管理器和实体管理器有什么区别

实体框架为在 SQL Server 对象资源管理器中显示数据的本地数据库返回空数据

为什么即使片段已添加到视图中,片段管理器也返回null?

当我在飞行模式下启动应用程序时,为什么位置管理器在“ locationManager:didUpdateLocations:”中返回一个位置?

为什么我的Odoo 11实例的数据库管理器已被禁用?

为什么我们必须使用依赖管理器?

为什么我的jdbc的Spring事务管理器无法正常工作?

为什么我的“任务管理器性能”选项卡微型图变成了点?

当我可以从 CLI 连接时,为什么不能从网络管理器连接 OpenVPN?

为什么我在GNOME顶部栏中看不到网络管理器图标?

为什么我的任务管理器性能图中只有一个核心?

为什么我的USB设备不会显示在设备管理器中?

为什么我的启动管理器上有两个内核?

为什么我无法从连接管理器启动Deluge守护程序?

为什么实体管理器在Spring Boot测试父级中不为空,而在存储库中为空?

多個列表理解使用上下文管理器返回空列表

位置管理器在iOS中的伊朗中返回空值

我的屏幕管理器无法返回主屏幕

为什么我的 for...of 循环返回空数组,Javascript 节点

为什么我的Ajax请求从wordpress返回空对象?

为什么我的JavaScriptSerializer返回空的json对象?

为什么我的豆返回空内调用@Autowired场?

为什么我的n皇后代码返回空列表?

为什么我的mysql查询总是返回空结果?