【发布时间】:2019-11-01 21:32:39
【问题描述】:
我的 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>
这是我的员工会话 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 + " ]";
}
}
(别介意法语哈哈)。
无论如何,如果有人看到实体管理器返回空列表的原因,那将对我有很大帮助,让我明白我在哪里犯了愚蠢的错误。
非常感谢大家,祝你有美好的一天,
票价。
【问题讨论】:
标签: java xml hibernate jpa jakarta-ee