gpt4 book ai didi

java - 为什么我的实体管理器返回空结果列表?

转载 作者:行者123 更新时间:2023-12-01 22:10:12 25 4
gpt4 key购买 nike

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

对于我的学校项目,我们必须从经典的 JDBC 集成迁移到 JPA 集成。就我自己而言,我决定使用 Hibernate JPA 框架。我已经在 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>

这是我的员工 session 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 + " ]";
}
}

(别介意法语哈哈)。

无论如何,如果有人明白为什么实体管理器返回空列表,那将对我有很大帮助,并使我明白我在哪里犯了愚蠢的错误。

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

票价。

最佳答案

如果您将上述 session bean 的相同代码复制/粘贴到 servlet 中,请注意事务管理。由于 session bean 将事务管理为其默认行为,因此您不必在 bean 中管理它们。然而 servlet 没有这样的行为,实现它们是您自己的责任。

请注意,在属于 View 层的 servlet 内实现与数据库相关的作业将是一种反模式。更好的方法是使用 session bean 来实现此功能,并在 View 层内调用它们。

关于java - 为什么我的实体管理器返回空结果列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58666156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com