Francis
Francis

Reputation: 201

java.lang.ClassCastException: java.lang.Long cannot be cast to [Ljava.lang.Long; on iterating through values from the db

I am trying to fetch the last data in a table using hibernate as shown in the following snippets

query.setMaxResults(1);
         List<Long[]> rows =  query.list();
         System.out.println("current row>>>>>>  " +rows.toString());

          for (Long[] row: rows) {
                System.out.println(" ------------------- ");
                long val = (Long) row[6];
                System.out.println("current file: " + val);
            }

          }catch(Exception ex){
              ex.printStackTrace();
          }

this is my query

Query query = session.createQuery("select f.currentfile.id from File f order by f.id DESC");

I am having the error at this line of the code

java.lang.ClassCastException: java.lang.Long cannot be cast to [Ljava.lang.Long;
for (Long[] row: rows) {

Please what could be wrong?

Upvotes: 0

Views: 2754

Answers (2)

Shantaram Tupe
Shantaram Tupe

Reputation: 1666

remove query.setMaxResults(1);

otherwise u will get single result

try this

Edited

     List<Long> rows =  query.list();
     System.out.println("current row>>>>>>  " +rows.toString());

      for (Long val: rows) {
            System.out.println(" ------------------- ");
            System.out.println("current file: " + val);
      }

Upvotes: 1

ravthiru
ravthiru

Reputation: 9633

Your Query returns List<Long> and you are assigning List<Long[]>

Long is java.lang.Long

Long[] is [Ljava.lang.Long

Upvotes: 3

Related Questions