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
The problem are in syntax, you have not written it in a right way.
The correct one:
To have more details about it you have to read the documentation
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: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.
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:And while using native query, the query should be updated as below:
I suggest that you go through the documentation for deeper understanding on this topic.