2

I am trying to access list of friends of given username using hibernate. Here is my Service class in which fetchListOfFriends function is used to convert the generic list to an Arraylist of type of FriendsDetails.

@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)
11
  • What does the line System.out.println(obj.toString()); print in console? Commented May 10, 2016 at 16:53
  • 1
    which method is throwing the exception, and what line is causing the ClassCastException? Commented May 10, 2016 at 17:08
  • @chsdk No, it is not able to print anything.. till creating a new list 'listOfFriend' the code is executing.. inside while loop there is some error... Commented May 10, 2016 at 17:15
  • @alexanderific 2nd method.. while loop is not able to run Commented May 10, 2016 at 17:15
  • 1
    According to the stacktrace, the problem is probably in FriendServiceImpl on line 61. Commented May 22, 2016 at 8:12

1 Answer 1

0

Everything should work correctly.

The problem, obviously, that List listOfFriends is List<String>, not List<Object[]>. It is strange, because of Select name,presenceStatus from UserPresence should return List<Object[]>.

Maybe Spring set an incorrect implementation of @Autowired private DetailsDao detailsDao;.

Try this code. This should work for 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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.