Hibernate hql Query exception: java.lang.ClassCastException resolution

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.