我需要一个自定义查询在我的存储库扩展crudrepository
@Repository
public interface SongListRepository extends CrudRepository<SongList, Integer> {
@Query("SELECT sl FROM song_list sl WHERE sl.owner=:owner")
Set<SongList> findAllSongListsForUser(
@Param("owner") String owner);
}
当我尝试启动时,会抛出以下例外:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'songListController': Unsatisfied dependency expressed through field 'songListService';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'songListService': Unsatisfied dependency expressed through field 'repository';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'songListRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.Set htwb.ai.songsservice.repository.SongListRepository.findAllSongListsForUser(java.lang.String)!
如何在crudepository中创建自定义查询?
2条答案
按热度按时间kzmpq1sx1#
您不需要创建自己的查询。只需使用基于方法签名的自动生成查询。
这将创建查询。
yuvru6vn2#
使用jpql,或者如果您想在代码中使用sql,请使用本机查询,如下所示: