0

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?

7
  • Ljava.lang.Long means List of java.lang.Long Commented Sep 13, 2016 at 11:53
  • what is your query, what you are returning Commented Sep 13, 2016 at 11:54
  • query has just been added Commented Sep 13, 2016 at 11:55
  • According to your querry (assuming that the id of a file is a long) you'll get a List<Long> and not a List<Long[]>. Try: List<Long> rows = query.list(); Commented Sep 13, 2016 at 11:57
  • also you have set max result to 1, it will return single Long Commented Sep 13, 2016 at 12:02

2 Answers 2

3

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

Long is java.lang.Long

Long[] is [Ljava.lang.Long
Sign up to request clarification or add additional context in comments.

3 Comments

List<Long> rows = query.list();
Type mismatch: cannot convert from element type Long to Long[] Change type row to Long
you also need to change row to Long in for loop
1

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);
      }

1 Comment

Type mismatch: cannot convert from element type Long to Long[] Change type row to Long

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.