skip to Main Content

My application runs fine when it doesn’t include any custom method in Repository. The moment I add a custom repo method in repo interface, it gives exceptions and application fails to start.

package com.library.repo;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.library.entity.ShowEntity;

@Repository
public interface ShowRepository extends JpaRepository<ShowEntity, Integer> {

    
    @Query("SELECT s FROM show_entity s WHERE s.movie.movie_id = :movieId")
    List<ShowEntity> findShowsByMovieId(int movieId);

}

Console message:

Error creating bean with name 'showController' defined in file [/Users/PR20372898/Documents/workspace-spring-tool-suite-4-4.15.1.RELEASE/XYZCinemas/target/classes/com/library/controller/ShowController.class]: Unsatisfied dependency expressed through constructor parameter 0: 

Error creating bean with name 'showService' defined in file [/Users/PR20372898/Documents/workspace-spring-tool-suite-4-4.15.1.RELEASE/XYZCinemas/target/classes/com/library/service/ShowService.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'showRepository' defined in com.library.repo.ShowRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: 

Could not create query for public abstract java.util.List com.library.repo.ShowRepository.findShowsByMovieId(int); Reason: Validation failed for query for method public abstract java.util.List com.library.repo.ShowRepository.findShowsByMovieId(int)

What could be the possible reasons for getting this problem? I have used all the annotations. Please help.

3

Answers


  1. The problem are in syntax, you have not written it in a right way.

    The correct one:

    @Query("SELECT s FROM ShowEntity s WHERE s.movie.movie_id = :movieId")
        List<ShowEntity> findShowsByMovieId(@Param("movieId") Integer movieId);
    

    To have more details about it you have to read the documentation

    Login or Signup to reply.
  2. It looks like there may be a bug in your query syntax. If you are trying to select all columns from the show_entity table, the syntax should be:

    @Query("SELECT * FROM show_entity s WHERE s.movie.movie_id = :movieId")
    List<ShowEntity> findShowsByMovieId(@Param("movieId") int movieId);
    
    Login or Signup to reply.
  3. The Spring Data JPA already provides support to dervied queries based on method names. That can directly be used in this case as shown below to retrieve records by movie ID.

    @Repository
    public interface ShowRepository extends JpaRepository<ShowEntity, Integer> {
        List<ShowEntity> findByMovieId(int movieId);
    }
    
    

    But if you still wish to use the @Query annotation, you need to understand the difference between JPQL (Java Persistence Query Language) and native queries. The query can be rewritten in JPQL as:

    @Query("SELECT s.* FROM ShowEntity s WHERE s.movieID = :movieId")
    List<ShowEntity> findShowsByMovieId(@Param("movieId") int movieId);
    

    And while using native query, the query should be updated as below:

    @Query("SELECT s.* FROM show_entity s WHERE s.movie_id = :movieId", nativeQuery = true)
    List<ShowEntity> findShowsByMovieId(@Param("movieId") int movieId);
    

    I suggest that you go through the documentation for deeper understanding on this topic.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search