java.lang.ClassCastException:java.lang.String无法强制转换为[Ljava.lang.Object;

时间:2016-05-10 16:48:05

标签: spring hibernate list arraylist

我正在尝试使用hibernate访问给定用户名的朋友列表。这是我的Service类,其中fetchListOfFriends函数用于将通用列表转换为FriendsDetails类型的Arraylist。

@Service
@Transactional
public class DetailsServiceImpl implements DetailsService {
@Autowired
private DetailsDao detailsDao;
@Override
public List<FriendsDetails> fetchListOfFriends(String name) {

    @SuppressWarnings("rawtypes")
    List listOfFriends=detailsDao.fetchListOfFriends(name);
    List<FriendsDetails> friendList= fetchListOfFriendss(listOfFriends);
    if(listOfFriends==null){
        System.out.println("Empty and null list");

    }
    System.out.println("size of friendList" + listOfFriends.size());
    return friendList;
}
private List<FriendsDetails> fetchListOfFriendss(@SuppressWarnings("rawtypes") List genericList) {

    @SuppressWarnings("unchecked")
    List<Object> result = (List<Object>) genericList; 
    Iterator<Object> itr = result.iterator();
    List<FriendsDetails> listOfFriend= new ArrayList<FriendsDetails>();
    while(itr.hasNext()){
       Object[] obj = (Object[]) itr.next();
       System.out.println(obj.toString());
       String userName = String.valueOf(obj[0]); 

       FriendsDetails obj1= new FriendsDetails();
       obj1.setFriendName(userName);

       listOfFriend.add(obj1);
    }
    return listOfFriend;
}

DetailsDaoImpl.java

@Autowired
private SessionFactory sessionFactory;
@SuppressWarnings("rawtypes")
@Override

 public List fetchListOfFriends(String name) {
    Session session=sessionFactory.getCurrentSession();
    String queryToFetchFriends="Select name,presenceStatus from UserPresence where name in (Select friendName from Friends where name='"+name+"')";
    List listOfFriends=session.createSQLQuery(queryToFetchFriends).list();
    return listOfFriends;
}

Logs.txt

May 22, 2016 1:24:11 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [springmvc] in context with path        [/Testing] threw exception [Request processing failed; nested exception is        java.lang.ClassCastException: java.lang.String cannot be cast to     [Ljava.lang.Object;] with root cause
 java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
at com.tcs.Service.FriendServiceImpl.searchFriend(FriendServiceImpl.java:61)
at com.tcs.Service.FriendServiceImpl.searchFriend(FriendServiceImpl.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

1 个答案:

答案 0 :(得分:0)

一切都应该正常工作。

问题显然是List listOfFriendsList<String>,而不是List<Object[]>。这很奇怪,因为Select name,presenceStatus from UserPresence应该返回List<Object[]>

也许Spring设置了@Autowired private DetailsDao detailsDao;的错误实现。

试试这段代码。这适用于List<String>

private List<FriendsDetails> fetchListOfFriendss(List<?> genericList) {
            Iterator<?> itr = genericList.iterator();
            List<FriendsDetails> listOfFriend = new ArrayList<FriendsDetails>();

            while (itr.hasNext()) {
                Object obj = itr.next();
                String userName = String.valueOf(obj);
                System.out.println(userName);

                FriendsDetails obj1 = new FriendsDetails();
                obj1.setFriendName(userName);

                listOfFriend.add(obj1);
            }
            return listOfFriend;
}