Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA/Boot: findBy ... or

I want to write a finder method in my repository to find an object based on one field OR another one while supplying one parameter like:

@RepositoryDefinition(domainClass = Person.class, idClass = Long.class)
public interface PersonRepository extends CrudRepository<Person, Long> {

    List<Person> findAllByIdOrAnotherId(someId);
}

How can I do that without using SQL?

like image 331
biniam Avatar asked Oct 13 '16 14:10

biniam


People also ask

Should I use JpaRepository or CrudRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

What is the difference between Findby and findAll in JPA?

No, there is no difference between them, they will execute exactly the same query, the All part is ignored by Spring Data when deriving the query from the method name.

What is Spring data JPA in spring boot?

JPA is a specification which specifies how to access, manage and persist information/data between java objects and relational databases. It provides a standard approach for ORM, Object Relational Mapping. Spring Boot provides a seemless integration with JPA.


1 Answers

I added a second parameter to the method and it worked.

List<Transaction> findAllByIdOrParentId(Long id, Long parentId);

This is just a definition for the method because I pass the same parameter to the method from the service as:

List<Transaction> transactions = transactionRepository.findAllByIdOrParentId(transactionId, transactionId);
like image 171
biniam Avatar answered Oct 30 '22 19:10

biniam