Spring Boot Mysql query to get all records based on the @query value

1.4k Views Asked by At

I am trying to retreive all records from a MySQL table using spring boot JPA. Following is the repositry definition

MovieRepository class

@Query("From MovieEntity m, TheaterMovieShowEntity tms, TheaterEntity t where t.city=?1 and t.theaterid=tms.theaterid and m.movieid=tms.movieid and t.seatsavailable>=?2")
    List<Movie> getMoviesDetails(String cityName, int noOfTickets)throws MovieException;

MovieService Class

public List<Movie> getMoviesDetails(String cityName, int noOfTickets)throws MovieException{
        List<Movie>moviesIntheCityList = new ArrayList<Movie>();
        **moviesIntheCityList = (List<Movie>) movieRepository.getMoviesDetails(cityName, noOfTickets);**
        System.out.println("in the getMoviesDetails after querying"+moviesIntheCityList); // This is null
        return moviesIntheCityList;
    }

What am i missing?

2

There are 2 best solutions below

0
On

In your Repository File use this way

List<Movie> findByCityNameAndNoOfTickets(String cityName, int noOfTickets)

refer Query Creation

1
On

Just use

List<Movie> movieRepository.findByCityNameAndNoOfTickets(String cityName, int noOfTickets);

in your MovieRepository. And then call it. In Spring Data JPA if you follow proper naming conventions then you got to only declare the name of method in Repository and JPA will implement it for you.