在Hibernate返回的List上调用List#toArray()时的ArrayStoreException

时间:2013-01-07 13:39:26

标签: java arrays hibernate

我正在尝试将Hibernate返回的List转换为UserDetails[]数组,如下所示:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
List<UserDetails[]> oo = session.createSQLQuery("SELECT * FROM `userdetails` WHERE 1 ").list();
System.out.println(oo.size()); // Until here it works fine and it shows me the rows I asked in my query.
UserDetails[]us = oo.toArray(new UserDetails[oo.size()]); // Here it throws java.lang.ArrayStoreException
session.close();

然而,它会抛出java.lang.ArrayStoreException。这是怎么造成的,我该如何解决?

2 个答案:

答案 0 :(得分:0)

您可以直接打印清单:

for(UserDetails list: oo){
    System.out.println("Result"+ list.toString());
}

或获取特定数据

for(UserDetails list: oo){
    System.out.println("Result"+ list.getId());
}

答案 1 :(得分:0)

该错误表示已尝试将错误类型的对象存储到对象数组中。见http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ArrayStoreException.html