Custom Query Spring Data JPA

Viewed 415

I need a custom Query in my Repository which extends 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);
}

When I try to start the following excpetion is thrown:

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)!

How do i create custom Querys in a CrudRepository?

1 Answers

You don't need to create your own query. Simply use the auto generate query based on the method signature.

List<SongList> findAllByOwner(String owner);

This will create the query.

Related