-1

Why does this cast fail? Since results for me is a List of Average. But apparently some magic is needed since java thinks it's an Object.

When working further with this it seems like the q.getResultList() call will give me a raw list of object which seems strange to me, since it's a TypedQuery.

TypedQuery<Average> q = em.createQuery("select p.id, p.lastName, p.firstName, c.name, s.serieDate, st.serie_type, st.serie_type_info, count(s.player.id), sum(s.result), avg(s.result) from Serie s, Player p, Club c, SerieType st " +
        " where p.id = s.player.id" +
        " and c.id = s.club.id " +
        " and st.id = s.serieType.id " +
        " and c.name like '%" + clubName + "%'" +
        " and st.serie_type like '%" + serieType.getSerie_type() + "%'" +
        " and st.serie_type_info like '%" + serieType.getSerie_type_info() + "%'" +
        " and s.serieDate > :startDate" +
        " and s.serieDate < :endDate" +
        " group by p.id, p.lastName, p.firstName, c.name, s.serieDate, st.serie_type, st.serie_type_info", Average.class);
        q.setParameter("startDate", calendarBean.getDate1());
        q.setParameter("endDate", calendarBean.getDate2());

Here is the call

List<Average> resultList = q.getResultList();
List<Average> withAverages = CalculateAveragesClub(resultList);

private List CalculateAveragesClub(List<Average> results)   {
        for (Average average : results) { //error in this cast
}

SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/BowlingFacelets] threw exception [[Ljava.lang.Object; cannot be cast to com.jk.hcp.Average] with root cause
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.jk.hcp.Average
    at com.jk.hcp.ServiceSeries.CalculateAveragesClub(ServiceSeries.java:508)
user2130951
  • 2,601
  • 4
  • 31
  • 58

2 Answers2

1

A little more searching and I would have found

JPQL Create new Object In Select Statement - avoid or embrace?

credits to Xavi for leading me in the right direction.

Community
  • 1
  • 1
user2130951
  • 2,601
  • 4
  • 31
  • 58
0

With the code and the error you posted, it looks like that method is expecting a List<Average>, but the elements in the List are Object[] actually (at least one of them is).

Here's an Ideone sample reproducing the problem with the String class instead of Average.

Make sure to have Average objects in the List.

Type safety is not enforced on the code calling that method because of q.getResultList() returning a raw List (without generic type).

List<Average> resultList = q.getResultList();

EDIT According to the edit, it looks like the List is returned by a JPA/Hibernate Query. If it is an Hibernate SQLQuery along the lines of SELECT * from average_table, make sure to call addEntity() (in any of its convenient flavors) on the SQLQuery so that it returns Average objects instead of Object arrays with all the columns in the query (or use HQL/Criteria if suitable).

q.addEntity(Average.class);
List<Average> resultList = q.getResultList();

You might find Chapter 16 - Native SQL of the Hibernate Reference useful, specially 16.1.2. Entity queries.

Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • Seems to refer to jboss entities. It's a Derby database and eclipselink is used for JPA. – user2130951 Jan 28 '14 at 11:05
  • 1
    @user2130951 Not familiar with `TypedQuery`, but it seems JPA won't know how to convert that collection of selected properties to an `Average` instance automagically. Is there an `Average` constructor using those fields? If there is so, you could try `SELECT new Average(p.id, ...) FROM ...` – Xavi López Jan 28 '14 at 11:36
  • @user2103951 Glad you've solved it. Would you mind posting and accepting an answer with the details so that future visitors of the question can find a suitable accepted answer? Remember upvoting answers that were helpful and accepting the best answer is a great way of contributing to the site and improving its quality, while giving back :-) – Xavi López Jan 28 '14 at 12:31
  • I am not sure that this is anything to post. The solution seems really backwards to me and not at all something I would recommend unless there is absolutely no other way to do it. Since I have to send in an Object[] and cast everything while creating an Average object. – user2130951 Jan 28 '14 at 13:13