Sometimes, our query only needs to query a part of the bean without all its properties, and HQL can write it this way:
Suppose there is a class user (omit Getter Setter):
public class User {private String name;private int age;private boolean sex;}If you only need to query the name and age, HQL can write this: select New User (name,age) from user;
In this case, a constructor of the new User (Name,age) is required in the entity class.
The problem is, sometimes our entity class and some Lib Java class will have conflict, such as my problem is: I have a bean called member, the results of the above hql when the reported java.lang.ClassCastException exception.
Cause: There is a Java class with the same name, which is one of the causes of this exception.
Workaround: Add a specific path to the bean: such as select New Com.exampley.User (name,age) from Com.exampley.User;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hibernate hql Query exception: java.lang.ClassCastException resolution