0

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.

ks gujjar
  • 161
  • 1
  • 5

4 Answers4

1

I think the problem lies in the fact that you're explicitly selecting columns, causing Hibernate to not map the results to User objects.

This should work:

Query query=session.createQuery("from User");
List<User> users=query.list();
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • The solution which you have given, It will display all columns but I don't wanna display all columns , I wanna display only two columns 'name' and 'id'. So the query which i have written is correct but there is mistake somewhere in storing the return type of 'query.list()' method. – ks gujjar Jan 19 '17 at 05:02
  • @ksgujjar If you use your own query then hibernate will not match it to an Entity - see my links above – Scary Wombat Jan 19 '17 at 05:05
  • @Scary Wombat It is not my query , it is hibernate Specified query. – ks gujjar Jan 19 '17 at 05:27
  • `Query query=session.createQuery("select name,id from User");` **is** your query – Scary Wombat Jan 19 '17 at 05:28
  • @Scary Wombat If the query I have written, is not correct then what query should i write for retrieving only two columns out of three columns. – ks gujjar Jan 19 '17 at 05:50
  • @ksgujjar Why is retrieving that extra column such a big deal? Let me guess: you're storing a plaintext password? – Robby Cornelissen Jan 19 '17 at 05:52
  • 1
    @ksgujjar Please read carefully. If you want your own queries, then hibernate can not map them to Entities. You can write your own queries (as you have already done so) BUT these can not be mapped by hibernate to Entities and the columns will be mapped to an Object array. – Scary Wombat Jan 19 '17 at 06:06
  • @RobbyCornelissen see also http://stackoverflow.com/questions/2355728/jpql-create-new-object-in-select-statement-avoid-or-embrace/2359214 – Scary Wombat Jan 19 '17 at 06:31
-1

try this

List<User> users=(User)query.list();
  • Sorry sir , your answer is wrong because 'query.list()' method always return List of objects it cannot be casted to User Type. – ks gujjar Jan 19 '17 at 05:08
-1

Use alias name if you are using projection in hql. change your query to this and it should work.

 Query query=session.createQuery("select usr.name,usr.id from User usr");
Chandra
  • 1,722
  • 16
  • 19
-2

Thanks guys! I have found the solution to my question.

It is like this:

Query query = session.createQuery("select name,id from User");
List<Object> details = query.list();

for(Object detail : details)
{
    Object[] items = (Object[])detail;
            System.out.println(items[0]+"  "+items[1]);
}

It will display only two columns instead of three columns.

The main advantage of this procedure is that if there is big table containing 1000 columns and we want to retreive approx 10 columns, then we should use this process.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ks gujjar
  • 161
  • 1
  • 5