Ljava.lang.对象;无法强制转换为类 org.academic.server.entities.StudentEntity (JPA)

Ljava.lang.Object; cannot be cast to class org.academic.server.entities.StudentEntity (JPA)

提问人:Hubertius 提问时间:6/18/2023 最后编辑:Hubertius 更新时间:6/19/2023 访问量:42

问:

我正在尝试将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);
Java 对象 JPA 服务器 转换

评论

0赞 Marc Le Bihan 6/18/2023
如果你尝试一个而不是一个?TypedQuery<StudentEntity>Query
0赞 Hubertius 6/18/2023
@Marc Le Bihan:你能举几个例子吗?目前对于此类代码:“ TypedQuery<StudentEntity> queryForGettingStudent = databaseJpaHandler.getEntityManager().createNamedQuery(”SELECT * FROM Students S WHERE S.Email = :email “, StudentEntity.class) .setParameter(”email“, studentGradesRequestData.getEmail();”我得到这样的输出:“NativeQueryImpl 无法转换为类 javax.persistence.TypedQuery(org.hibernate.query.sql.internal.NativeQueryImpl 和 javax.persistence.TypedQuery 位于加载器”app“的未命名模块中)”
0赞 Marc Le Bihan 6/18/2023
TypedQuery<StudentEntity> typedQuery = getEntityManager().createQuery("SELECT u FROM StudentEntity u WHERE u.id=:id", StudentEntity.class);或也应存在。createNamedQuery
0赞 Manfred 6/18/2023
你的结果是一个数组(这就是前面的意思),你试图将其转换为单个 .哪一个是 160 行?[java.lang.ObjectStudentEntity
0赞 Manfred 6/18/2023
那可能是来自你的.什么?SELECT UD.Shortcut, Subj.Name, P.FirstName + ' ' + P.LastName, SS.Grade, Subj.ECTS, Subj.SpecificationStudentSubjectResponseDto

答: 暂无答案