简体   繁体   English

在JPA中使用GROUP BY子句时发生ClassCastException

[英]ClassCastException while using GROUP BY clause in JPA

I am working with JPA. 我正在与JPA合作。 while doing GROUP BY clause example it's throwing ClassCastException. 在执行GROUP BY子句示例时,将引发ClassCastException。

Below Is My code: 以下是我的代码:

public class StudentGrouping 
{
public static void main(String[] args)
{
    EntityManager entityManager = EntityManagerUtil.getEmf()
            .createEntityManager();
    try {
            EntityTransaction entr = entityManager.getTransaction();
            entr.begin();
            Query query = entityManager
                    .createQuery("SELECT student.studentName, SUM(student.studentAge) FROM Student student GROUP BY student.studentName");
            List<?> list = query.getResultList();
            Iterator<?> iterator = list.iterator();
            while (iterator.hasNext())
            {
                System.out.println("entered into loop");
                Student student = (Student) iterator.next();
                System.out.print("Student Name:"+student.getStudentName());
                System.out.print(" Age:"+ student.getStudentAge());
                System.out.println();

        }
        entr.commit();
        System.out.println("success");
    } 
    catch (Exception e)
    {
        e.printStackTrace();
    } finally {
        entityManager.close();
    }

}

}

Below is The Expection: 以下是期望:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.demo.entities.Student
at com.demo.action.StudentGrouping.main(StudentGrouping.java:28)

My POJO class Fields are: 我的POJO类字段是:

@Column(name = "studentName")
private String studentName;
@Column(name = "studentAge")
private int studentAge;

Is my GROUP BY clause query is wrong. 是我的GROUP BY子句查询错误。

Student student = (Student) iterator.next(); is the problem as you aren't actually pulling back an entire student. 这是问题所在,因为您实际上并没有撤回整个学生。

SELECT student.studentName, SUM(student.studentAge) FROM Student student GROUP BY student.studentName SELECT student.studentName,SUM(student.studentAge),来自Student Student GROUP BY,student.studentName

Your query is pulling back these two fields. 您的查询正在拉回这两个字段。 If you want to map to a student object, you'd have to use the following. 如果要映射到学生对象,则必须使用以下内容。

FROM Student student 来自学生

Then do the calculation by hand on the data. 然后手动对数据进行计算。 If you wanted to use your original query, you'd have to parse each individual value with, rather than using iterator lets say they were in a resultList. 如果要使用原始查询,则必须使用解析每个单独的值,而不是使用迭代器来说它们在resultList中。

for (Object[] result : resultList) {
 String studentName = (String) result[0]
 Integer age = (Integer) result[1];
}

This is because I am sure that you Student class does not look like 这是因为我确定您的Student班级看起来不像

  String studentName;
  Integer yyyy;

which is what you are getting when you ask for 当您要求时得到的是什么

  student.studentName, SUM(student.studentAge)

I suggest that you either create a class that looks like your result, or just treat the result as an Object[] 我建议您要么创建一个看起来像您的结果的类,要么将结果视为Object []

As in

Object student[] = (Object[])iterator.next();
System.out.print("Student Name:"+student[0]);
System.out.print("Max Age:"+student[1]);

Unless you have defined the Student class to be a String and a number, you're not getting back a Student class, so you can't cast it to that in this line: 除非您将Student类定义为String和数字,否则您不会返回Student类,因此无法在此行中将其强制转换为:

Student student = (Student) iterator.next();

That, and you probably should also define what kind of classes your List and Iterators are handling. 这样,您可能还应该定义List和Iterators正在处理的类。

The problem is you are actually not selecting the student instead you are trying to select custom values from student. 问题是您实际上没有选择学生,而是尝试从学生中选择自定义值。

try this: First create a constructor inside the entity with two param ie. 试试看:首先在实体内部创建一个带有两个参数的构造函数,即。 the name and age. 名字和年龄。

then use this query: 然后使用以下查询:

SELECT NEW com.sample.Student(student.studentName, SUM(student.studentAge)) FROM Student student GROUP BY student.studentName; 从学生学生组按student.studentName选择新的com.sample.Student(student.studentName,SUM(student.studentAge));

You need to specify the full class name (sample:com.sample.Student) in the query 您需要在查询中指定完整的类名(sample:com.sample.Student)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM