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?

1
  • this is Spring Data JPA not the JPA API. i.e there is no such JPA @Query. perhaps tag your question correctly Commented Oct 6, 2017 at 12:43

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

But what result would this query give me? On which column is distinct condition applied?
i updated my answer. Distinct work on the whole row. You acn also replace distinct with a group by

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.