Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring data querying with complex object marked by @Param

I have an entity with aggregation information that I am going to receive from database:

class BookStats {
    String author
    String title
    Integer count
}

My question is could I use some complex object in Repository to filter statistic information. Something like that:

 @Query(value = "SELECT new com.test.book.BookStats(b.author, b.title, count(b)) from Book b where b.title = :filter.title and b.author= :filter.author")
    List<BookStats> calculateBookStats (@Param("filter") Filter filter)
like image 250
Uladzislau Kaminski Avatar asked Oct 28 '25 06:10

Uladzislau Kaminski


1 Answers

Spring Data JPA allows to use SpEL :

@Query(value = "SELECT new com.test.book.BookStats(b.author, b.title, count(b)) from Book b where b.title = :#{#filter.title} and b.author= :#{#filter.author}")
List<BookStats> calculateBookStats (@Param("filter") Filter filter)

More info here.

like image 170
Aliaksei Stadnik Avatar answered Oct 30 '25 15:10

Aliaksei Stadnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!