提问人:Hubertius 提问时间:6/18/2023 最后编辑:Hubertius 更新时间:6/19/2023 访问量:42
Ljava.lang.对象;无法强制转换为类 org.academic.server.entities.StudentEntity (JPA)
Ljava.lang.Object; cannot be cast to class org.academic.server.entities.StudentEntity (JPA)
问:
我正在尝试将MySQL数据库的“对象”实例本机查询结果转换为实体类。在收到来自“客户端”的请求后,这种情况发生在“服务器”应用程序中。 基本上,有问题的代码片段在那一刻看起来是这样的:
else if(operation == GET_STUDENT_SUBJECTS_DATA) {
System.out.println("I'm getting students subjects data!");
StudentSubjectRequestDto studentGradesRequestData = (StudentSubjectRequestDto) inStream.readObject();
Query queryForGettingStudent = databaseJpaHandler.getEntityManager().createNativeQuery("SELECT * FROM Students S WHERE S.Email = :email ")
.setParameter("email", studentGradesRequestData.getEmail());
List<StudentEntity> listOfStudentsMatched = (List<StudentEntity>)(List<?>) queryForGettingStudent.getResultList();
if(!listOfStudentsMatched.isEmpty()) {
StudentEntity student = listOfStudentsMatched.get(0);
Query queryForGettingStudentSubjects = databaseJpaHandler.getEntityManager().createNativeQuery("SELECT UD.Shortcut, Subj.Name, P.FirstName + ' ' + P.LastName, SS.Grade, Subj.ECTS, Subj.Specification FROM StudentsSubjects SS INNER JOIN Students St on SS.StudentID = St.ID INNER JOIN Subjects Subj on SS.SubjectID = Subj.ID INNER JOIN University_Departments UD on St.DEPARTMENT_ID = UD.ID INNER JOIN Professors P on Subj.ProfessorID = P.ID " +
" WHERE St.Email := email")
.setParameter("email", student.getEmail());
if(sendStatusToClient(queryForGettingStudentSubjects, outStream, 10) > 0) {
List<StudentSubjectResponseDto> listOfStudentsResponse = queryForGettingStudentSubjects.getResultList();
outStream.writeObject(listOfStudentsResponse);
}
}
当有匹配并且“listOfStudentsMatched”在第一条指令中不为空时,如果我得到输出异常:
NFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Exception in thread "Thread-2" java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class org.academic.server.entities.StudentEntity ([Ljava.lang.Object; is in module java.base of loader 'bootstrap'; org.academic.server.entities.StudentEntity is in unnamed module of loader 'app')
I'm getting students subjects data!
Hibernate: SELECT * FROM Students S WHERE S.Email = ?
at org.academic.server.Server.run(Server.java:160)
学生实体.java
package org.academic.server.entities;
import jakarta.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
@Entity
@Table(name = "Students", schema = "academic_data")
public class StudentEntity implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "ID")
private int id;
@Basic
@Column(name = "Name")
private String name;
@Basic
@Column(name = "Surname")
private String surname;
@Basic
@Column(name = "Degree")
private String degree;
@Basic
@Column(name = "Semester")
private int semester;
@Basic
@Column(name = "PhoneNumber")
private String phoneNumber;
@Basic
@Column(name = "Address")
private String address;
@Basic
@Column(name = "Email")
private String email;
@Basic
@Column(name = "DEPARTMENT_ID")
private int departmentId;
@ManyToMany(mappedBy="students")
private Set<SubjectEntity> subjects = new HashSet<>();
public Set<SubjectEntity> getSubjects() {
return subjects;
}
public void setSubjects(Set<SubjectEntity> subjects) {
this.subjects = subjects;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public int getSemester() {
return semester;
}
public void setSemester(int semester) {
this.semester = semester;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getDepartmentId() {
return departmentId;
}
public void setDepartmentId(int departmentId) {
this.departmentId = departmentId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StudentEntity that = (StudentEntity) o;
return id == that.id && semester == that.semester && departmentId == that.departmentId && Objects.equals(name, that.name) && Objects.equals(surname, that.surname) && Objects.equals(degree, that.degree) && Objects.equals(phoneNumber, that.phoneNumber) && Objects.equals(address, that.address) && Objects.equals(email, that.email);
}
@Override
public int hashCode() {
return Objects.hash(id, name, surname, degree, semester, phoneNumber, address, email, departmentId);
}
}
这个例外背后的原因是什么? 根据我的理解,我得到了对象列表,可以转换为实体类。但这里似乎有问题。
编辑 这是有问题的行,我从中得到异常消息:
StudentEntity student = listOfStudentsMatched.get(0);
答: 暂无答案
下一个:无法投射到对象
评论
TypedQuery<StudentEntity>
Query
TypedQuery<StudentEntity> typedQuery = getEntityManager().createQuery("SELECT u FROM StudentEntity u WHERE u.id=:id", StudentEntity.class);
或也应存在。createNamedQuery
[
java.lang.Object
StudentEntity
SELECT UD.Shortcut, Subj.Name, P.FirstName + ' ' + P.LastName, SS.Grade, Subj.ECTS, Subj.Specification
StudentSubjectResponseDto