简体   繁体   English

本机查询返回带有java.lang.Object的列表,而不是JPA应用程序中的自定义对象类型

[英]Native Query returns list with java.lang.Object instead of custom object type in JPA application

I have the following tables in my MySQL database: 我的MySQL数据库中有以下表格:

Student
id | firstName | lastName | ...

Course
id | name | cp | ...
-

CourseResult (course_id and student_id are created by JPA)
id | course_id | student_id | grade | ...
-

I am trying to work with all Students who are in a Course defined by a given course_id . 我正在尝试与所有在给定course_id定义的Course Students course_id For that I've created a method in my controller: 为此,我在控制器中创建了一个方法:

public String showCourseResultList(Course course) {
    this.course = course;
    studentsInCourse = new ArrayList<Student>();
    studentsInCourse = em
            .createNativeQuery(
                    "SELECT * FROM Student WHERE id IN (SELECT student_id FROM CourseResult WHERE course_id = ?)")
            .setParameter(1, course.getId()).getResultList();
    System.out.println(studentsInCourse);
    return "courseResultList?faces-redirect=true";
}

So in the NativeQuery I am detecting all student_ids for a given course_id . 所以在NativeQuery中,我正在检测给定course_id所有student_ids Then I use the student_ids to select all Students from the Student table. 然后我用student_ids选择所有StudentsStudent表。 In other words I want to collect all Students in an ArrayList<Student> , who are in the given Course . 换句话说,我要收集所有Students在一个ArrayList<Student> ,谁在给定的Course

The problem is, that after doing that, the ArrayList<T> contains elements from the type java.lang.Object instead of my custom type Student . 问题是,这样做之后, ArrayList<T>包含来自java.lang.Object类型的元素,而不是我的自定义类型Student The System.out.println(studentsInCourse); System.out.println(studentsInCourse); returns [[Ljava.lang.Object;@3adaf419, [Ljava.lang.Object;@7ccfb37e] . 返回[[Ljava.lang.Object;@3adaf419, [Ljava.lang.Object;@7ccfb37e]

How can I collect all the Students from type Student in my ArrayList<T> instead of ArrayList<java.lang.Object> ? 我怎么能收集所有的Students从类型Student在我ArrayList<T>代替ArrayList<java.lang.Object>

Try to give the classname you expect as the second argument of your createNativeQuery call: 尝试给您期望的类名作为createNativeQuery调用的第二个参数:

studentsInCourse = em
            .createNativeQuery(
                    "SELECT * FROM Student WHERE id IN (SELECT student_id FROM CourseResult WHERE course_id = ?)", Student.class)
            .setParameter(1, course.getId()).getResultList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 数据 JPA – 自定义本机查询方法列表打印 java.lang.Object - Spring Data JPA – Custom Native Query-Methods list prints java.lang.Object Jpa储存库查询-java.lang.Object; 无法转换为模型 - Jpa Repository query - java.lang.Object; cannot be cast to model “选择新构造函数”查询返回 java.lang.Object 而不是构造函数实例 class - “select new constructor” query returns java.lang.Object instead of instance of constructors class 对于参数类型java.lang.Object,java.lang.Object,运算符&gt;未定义 - The operator > is undefined for the argument type(s) java.lang.Object, java.lang.Object 将 java.lang.Object 转换为自定义 object 会引发 ClassCastException - Casting java.lang.Object to custom object raises ClassCastException 如何将java.lang.Object转换为另一种类型的对象? - How to turn a java.lang.Object into another type of object? 无法从类型 [java.lang.Object[]] 转换为类型 [@org.springframework.data.jpa.repository.Query com.data.models.Users] - Failed to convert from type [java.lang.Object[]] to type [@org.springframework.data.jpa.repository.Query com.data.models.Users] XmlObject []到java.lang.Object [] - XmlObject [] to java.lang.Object[] 通用类的XStream反序列化返回java.lang.Object - XStream deserialization of generic class returns java.lang.Object Java - 从Java.lang.Object到自定义类的实例的类型转换 - Java - Typecasting from Java.lang.Object to an instance of a custom Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM