Chirrut Imwe
Chirrut Imwe

Reputation: 673

Group By function using Spring Data

I have a MySQL database, and I intend to implement a Group By query using Spring Data. (I'm really new to Spring Data)

i.e.

 Select Department, Count(*) From Employee Group By Department.

So far I've seen a lot of solutions here using MongoDb, but nothing on MySQL. I know you can use @Query annotation, but is there any other way I can achieve this?

Upvotes: 0

Views: 1069

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81882

For specifying queries with Spring Data you have the following options:

  1. Use one of the methods in the predefined interfaces like CrudRepository or JpaRepository. None of those does any grouping by.

  2. Use Query Methods where the name of the method defines the query. There is no keyword for group by either.

  3. Use a @Query annotation. This works, but you want an alternative.

  4. You can write a custom implementation. But why would you if you can achieve the same with the @Query annotation?

While there is technically an alternative, you probably want to use @Query in this case.

Upvotes: 2

Related Questions