I wanna display only two columns from User Table Using HQL. I have a User class and Main Class to display columns name and id only.
The User class is given below.(I have not written constructors and setters,getters here.)
@Entity
public class User
{
private String name;
@Id
private int id;
private String password;
/* Constuctors, setters and getters*/
}
And Main class is like this:-
public class Main
{
public static void main(String[] args)
{
SessionFactory sessionFactory =new Configuration().
configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
Query query=session.createQuery("select name,id from User");
List<User> users=query.list();
for(User user: users)
{
System.out.println(user.getId()+" : "+user.getName());
}
session.getTransaction().commit();
}
}
But it is showing following Exception :-
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.info.model.User at com.info.main.Main.main(Main.java:43)
NOTE:- I have create table user table already. This program is only for display the information.