0

The query is not returning all the records, i.e all the records whose count is same out of them only of is being returned. Where as the same code of MYSQL workbench works like a charm

JPA Custom Query

public interface BookingRepository extends JpaRepository<Booking, Long> {

    @Query("select count(v.source), concat(v.source,'-', v.destination) as bus_route from Booking v group by v.source, v.destination")
    public List<Object[]> groupByBus();
}

Output

Query in MYSQL

SELECT count(source), concat(source," - ", destination) as bus_route
FROM booking
GROUP BY source, destination;

enter image description here

As you can see there are two records with count of one, but only one is being returned by Spring data jpa

1
  • Just change the return type from List<Object[]> to List<Map<String, Object>> Commented Nov 18, 2020 at 19:05

1 Answer 1

1

Your query return a List<Object[]> but the object array could be almost everything.

Actually, Object[] contains, for each position, another Object[] with two values: count and bus_route.

You can iterate over every value in this way (I've tested and I've needed BigInteger to cast the object value):

Map<BigInteger,String> map = new HashMap<BigInteger,String>();
for(Object object[] : objectList) map.put((BigInteger)object[0], (String)object[1]);

And you will get the map you want.

Also, if there could be repeated values, only create a new list instead of Map.

Sign up to request clarification or add additional context in comments.

Comments

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.