自定义查询spring数据jpa

d4so4syb  于 2021-07-16  发布在  Java
关注(0)|答案(2)|浏览(390)

我需要一个自定义查询在我的存储库扩展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中创建自定义查询?

kzmpq1sx

kzmpq1sx1#

您不需要创建自己的查询。只需使用基于方法签名的自动生成查询。

List<SongList> findAllByOwner(String owner);

这将创建查询。

yuvru6vn

yuvru6vn2#

使用jpql,或者如果您想在代码中使用sql,请使用本机查询,如下所示:

@Query(value="SELECT sl FROM song_list sl WHERE sl.owner=:owner",nativeQuery=true)

相关问题