Announcement Announcement Module
No announcement yet.
ClassCastException iterating through getHibernateTemplate().find Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • ClassCastException iterating through getHibernateTemplate().find

    I am new to Spring and Hibernate and when I call
    List l = getHibernateTemplate().find(" select a, b, c from Cat");

    I try to iterate through the results and get a ClassCastException
    when I try to cast each object to a Cat.

    If I don't try to cast the results, I do get the fields I expect,
    they are just not in the object I expect. (I pass the List back
    to the front end to be displayed using javascript)

    Can someone point me in the right direction?

  • #2
    IN your query you state that you want just to return certain fields (a, b, c) from your Cat object. Your result will be a List of Object[] containing the results of each field.

    List l = getHibernateTemplate().find("from Cat");
    If you would that you would get a List of Cat objects.

    For more information read the Hibernate reference guide.


    • #3
      One more option

      Hi dmkinn,

      I agree with mdeinum, but I want to add one more possibility:

      List l = getHibernateTemplate().find("select new Cat(a, b, c) from Cat");
      For each element in the list, there would be a Cat instantiated by calling the constructor. Most likely there are more fields in Cat (otherwise you wouldn't be manually selecting certain fields), but assuming you have a three argument constructor that is compatible with the fields "a, b, c", you could instruct Hibernate to create a non-fully populated Cat.

      (A last possibility is to create a new, non-persistent object, CatSubset, and "new" that in your HQL instead. I could see doing this for component-specific queries so that you do not have to deal with an Object[].)

      -Arthur Loder


      • #4
        Thanks for the suggestions. I removed the select from my query and now I do get back a list of Cat objects. Now the problem is that I get the correct number of objects (18), but they all contain the same data. If I put the select back in I get the correct data for each row, just not Cat objects.

        Why should the select make a difference in the data I get back?


        • #5
          Iterating correctly?

          Hi dmkinn,
          Originally posted by dmkinn View Post
          I get the correct number of objects (18), but they all contain the same data.
          Are you sure you're iterating over the List correctly? There isn't a simpler HQL query than "from Cat", so I wouldn't think it's a Hibernate issue.

          Sample iteration using Java 5.0 generics:
          List<Cat> cats = getHibernateTemplate().find("from Cat");
          for (Cat cat : cats) {
          -Arthur Loder


          • #6
            Thanks I think I finally get it!

            Thanks for all the help. I finally figured out that I had the properties for the table set up incorrectly. I had an id set up for the table that was not unique. There is no primary key on this table, that is why if gave me the same data back every time.

            Thanks again!