0

I am trying to retrieve the values from database and storing it in List,

The data's are retrieved and working properly, But when i convert the List into Object<pojo class> i am getting an Exception ,

My Code is

Query qry=session.createQuery("select personaldetails.fname,personaldetails.lname from Personaldetails as personaldetails where refId=1001");
List<Personaldetails> l=(List<Personaldetails>)qry.list();
session.getTransaction().commit();
session.close();
System.out.println("--->"+l.size());  //List 'l' holds the values from DB
Personaldetails p; //This is an pojo class
p=(Personaldetails)l.get(i);  //Here i am getting the exception here
System.out.println("Person name "+p.getFname());

In above mentioned line i got Exception as ClassCastException , i don't know why, i tried it shows no error while compiling.

Any suggestion will be appreciated....

7
  • 1
    when you check actual type of l.get(i) in a debugger, what do you get ? Commented Sep 18, 2013 at 12:24
  • @Jerome I dont get any error , while debugging and the syntax is seems to be correct right,any alternative way is there to convert Commented Sep 18, 2013 at 12:26
  • You are not casting List to Object<pojo>, but are casting Object to Personaldetails and casting Object to List<Personaldetails>. Where is your exception being thrown? Commented Sep 18, 2013 at 12:26
  • @kark, I agree that your code compile cleanly, but it does not mean that l.geti) returns the right type. What is this type ? (cf. my question in first comment) Commented Sep 18, 2013 at 12:28
  • 1
    On a side note: "new Personaldetails()" - you don't use this as in the next line you are doing p=(Personaldetails)l.get(i); Commented Sep 18, 2013 at 12:31

2 Answers 2

5

I see that you are doing selective query - that is select fname, lname

select personaldetails.fname,personaldetails.lname from Personaldetails as personaldetails where refId=1001

which returns List<Object[]> where each elements in array represents the 2 column values

List will be something like [{"Fname1", "LName1"}, {"Fname2", "Lname2"}]

So ClassCastException is due to the fact that you are converting Object[] into PersonDetails.

  • To expect List<PersonDetails> as the result, you can use query like

    select from Personaldetails as personaldetails where refId=1001

  • Or you can iterate through the List<Object[]> and construct PersonDetails yourself

    for(Object[] arr : l) {
       PersonDetails p = new PersonDetails();
       p.setFName(arr[0]);
       p.setLName(arr[1]);
    }
    

I prefer the first approach

Sign up to request clarification or add additional context in comments.

3 Comments

Ya exactly you are right.. My question is how can i get the values from List<Object[]>
I can get the answer by the way what you mentioned. but i need only two columns not all columns.. I will try your second type
@sanbhat.. Your second type is quite nice. i got a clue for that. i used List<Object[]> insteadof List<Personaldetails> .. so i retrieved the data easily.. thanks for your time brother..
1

In above mentioned line i got Exception as ClassCastException , i don't know why, i tried it shows no error while compiling.

When you explicitly cast, the compiler cannot help you. Type casting is your way to tell the compiler that you know this type is some other type. The compiler trusts that you know what you're doing.

The real question here is how does the list get created?

Query qry=session.createQuery("select personaldetails.fname,personaldetails.lname from Personaldetails as personaldetails where refId=1001");
List<Personaldetails> l=(List<Personaldetails>)qry.list();

How do you know that qry.list() returns a List? You just ran a SQL statement and jumped to a List. Since the cast to List works fine, clearly a List is in fact returned (though due to type erasure, we don't know if it in fact only holds Personaldetails objects).

It must return a List. @sanbhat seems to have pointed you to the right structure for that method. I don't have any experience with Hibernate.

2 Comments

Thanks for your time.. ya accept your point. the things you have pointed out is nice.. the retrieving record from database and Query is correct . i am strucking in converting values types only
What I am saying is the conversion fails because the Query object is not returning what you think it is returning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.