0

I have added this in my DAO:

@Query("SELECT distinct LBL.cId, LBL from FooBar LBL ORDER BY LBL.collectedAt DESC")
    List<FooBar> findDistinctcIdOrderByCollectedAtDesc();

And then this is in my service

List<FooBar> fooBars = this.liveBusLocationDao.findDistinctcIdOrderByCollectedAtDesc();

And this is what I got when I debug contents of fooBars

fooBars = {ArrayList@11035}  size = 1
 0 = {Object[2]@11093} 
   0 = "string"
   1 = {FooBar@11095} "FooBar(cId=string, internalcId=123456789012, createdAt=2011-11-02 02:59:12.208, collectedAt=2017-10-06 14:26:26.544)"

How can I traverse this result?

nirvair
  • 4,001
  • 10
  • 51
  • 85
  • this is Spring Data JPA not the JPA API. i.e there is no such `JPA @Query`. perhaps tag your question correctly –  Oct 06 '17 at 12:43

2 Answers2

0

Your Query is requesting on 2 attributes. you can make a Custom class that as the id and the object or you could just remove LBL.cId from SELECT

Your request should be :

@Query("SELECT distinct LBL from FooBar LBL ORDER BY LBL.collectedAt DESC")
List<FooBar> findDistinctcIdOrderByCollectedAtDesc();

The distinct is working on the whole Row. take a look to it, if you are not shure about it, you can use group by LBL

https://stackoverflow.com/a/10066187/3543153

DamienB
  • 419
  • 1
  • 6
  • 20
0

You would need to create a result class and use special syntax in the jpa query if you want to use projection without getting the Object[] result.

package com.example

class ResultClass{

   int fieldOne;
   String fieldTwo;

  public (int fieldOne, intfieldTwo){
     // .. set fields
  }

}

and query:

@Query("SELECT distinct new com.example.ResultClass(LBL.cId, LBL.strProperty) from FooBar LBL ORDER BY LBL.collectedAt DESC")
    List<ResultClass> findDistinctcIdOrderByCollectedAtDesc();
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63