发生类别转换异常

时间:2016-03-31 14:27:43

标签: java spring hibernate

我收到了ClassCastException,我不知道为什么。这是我的代码:

public class RestLocationService {

    public static void main(String[] args) {
        getAllRestDetails();
    }

    public static List<RestLocation> getAllRestDetails() {

        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        List<RestLocation> restDetails = new ArrayList<RestLocation>();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();

            Query query = session.createQuery(" from RestLocation ");

            @SuppressWarnings("unchecked")
            List<Object[]> list = query.list();

            for (Object[] arr : list) {
                System.out.print(Integer.parseInt((String) arr[0]));
                System.out.println("\t" + arr[1].toString());
                System.out.println("\t" + arr[2].toString());
                System.out.println("\t" + arr[3].toString());
                System.out.println("\t" + arr[4].toString());
                System.out.println("\t" + arr[5].toString());
                System.out.println("\t" + arr[6].toString());
                System.out.println("\t" + arr[7].toString());
                System.out.println("\t" + arr[8].toString());
                System.out.println("\t" + arr[9].toString());
                System.out.println("\t" + arr[10].toString());
                System.out.println("\t" + arr[11].toString());
                System.out.println("\t" + arr[12].toString());

                RestLocation details = new RestLocation();
                details.setRestId(Integer.parseInt((String) arr[0]));
                details.setGeoX(arr[1].toString());
                details.setGeoY(arr[2].toString());
                details.setAddress1(arr[3].toString());
                details.setAddress2(arr[4].toString());
                details.setAddress3(arr[5].toString());
                details.setArea(arr[6].toString());
                details.setTown(arr[7].toString());
                details.setPostcode(arr[8].toString());
                details.setState(arr[9].toString());
                details.setCountry(arr[10].toString());
                details.setLandmark(arr[11].toString());
                details.setSmartCode(arr[12].toString());
                restDetails.add(details);
            }
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sessionFactory.close();
        }
        return restDetails;
    }
}

我收到的错误是这样的:

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate Configuration loaded
Hibernate serviceRegistry created
Hibernate: select restlocati0_.REST_ID as REST_ID1_0_, restlocati0_.ADDRESS_LINE1 as ADDRESS_2_0_, restlocati0_.ADDRESS_LINE2 as ADDRESS_3_0_, restlocati0_.ADDRESS_LINE3 as ADDRESS_4_0_, restlocati0_.AREA as AREA5_0_, restlocati0_.COUNTRY as COUNTRY6_0_, restlocati0_.GEO_X as GEO_X7_0_, restlocati0_.GEO_Y as GEO_Y8_0_, restlocati0_.LANDMARK as LANDMARK9_0_, restlocati0_.POSTCODE as POSTCOD10_0_, restlocati0_.ZIPPR_SMART_CODE as ZIPPR_S11_0_, restlocati0_.STATE as STATE12_0_, restlocati0_.TOWN as TOWN13_0_ from fhv1.restlocation restlocati0_
java.lang.ClassCastException: com.hitsquad.model.RestLocation cannot be cast to [Ljava.lang.Object;
    at com.hitsquad.service.RestLocationService.getAllRestDetails(RestLocationService.java:36)
    at com.hitsquad.service.RestLocationService.main(RestLocationService.java:18)

1 个答案:

答案 0 :(得分:0)

您查询返回RestLocation对象的列表。您将结果分配给Object []对象列表。当您尝试迭代列表时,每个元素都被强制转换为Object [],因此是一个ClassCastException。

将您的代码更改为

//*[not(node()]