find by id in spring data jpa Base Repository
19,777
I've just found a solution I'm satisfied with:
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID> {
List<T> findAll( );
Optional<T> findById(ID id);
@Query("SELECT t FROM #{#entityName} t WHERE t.id IN :ids")
List<T> findByIdsIn(@Param("ids") List<ID> ids);
}
I'm happy for other people to contribute if there are more elegant solutions, though.
Author by
Jimmy
Updated on June 04, 2022Comments
-
Jimmy about 2 years
I'm trying to implement a generic
findByIdsIn
method in theBaseRepository
of my Spring Data JPA class so that the functionality can be used by all the classes that implement that interface. I'm pretty new to Spring Data JPA so I'm not sure if that is possible and how to implement it.Here's the code I have so far. The
findByIdsIn
is the functionality I'd like to implement.import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.Repository; import java.io.Serializable; import java.util.List; import java.util.Optional; @NoRepositoryBean public interface BaseRepository<T, ID extends Serializable> extends Repository<T, ID> { List<T> findAll( ); Optional<T> findById(ID id); List<T> findByIdsIn(List<ID> ids); // This is the functionality I'd like to implement. }
Do you guys have any suggestions? Many thanks!
-
Vinay Prajapati over 6 yearsI have tried to put sufficient code to help you create a custom repository. But there should be some useful links too that explain in detail why you needed so many steps
-
Adesh Kumar over 6 yearswouldn't it require registering the BaseRepository with JPA?
-
Vinay Prajapati over 6 years@jimmy can you please accept answer to this question.
-
Vinay Prajapati about 6 yearswhoever downvoted, can you please help me understand the issue with this post.