TS How
TS How

Reputation: 81

Spring data repositories - parameter what to retrieve all records?

@Repository
public interface UserDao extends User {
   public List<User> findByFirstname(String firstname);
}

How could I use above code to retrieve all records? I tried findByFistname(null);, it doesn't work... I don't want to use findByFirstname(); because it's possible to have parameter.

Hope you all understand.

Upvotes: 8

Views: 26102

Answers (4)

Artem Novikov
Artem Novikov

Reputation: 4644

As I got from comments you're trying to achieve ignorance of null values of passed parameters (instead of retrieving all records by findAll()).

Unfortunately, currently, it's not supported by Spring .

You could leverage the @Query annotation and write the query manually in such manner:

@Query("select u from User u where "
    + "(:firstname is null or u.firstname = :firstname)"
    + "(:lastname is null or u.lastname = :lastname)"
)
public List<User> findUserByFirstNameAndLastName(
    @Param("firstname") String firstname, 
    @Param("lastname") String lastname
);

Upvotes: 1

razboy
razboy

Reputation: 1028

Have you considered using a spring data specification? In spring data a specification is a way to wrap the JPA criteria api.

The idea behind the JPA Criteria api is to generate queries programatically, by defining query objects.

Once you have encapsulated the criteria in a specification objects, a single findAll method can be used in a number of scenarios. For example programatically add criteria based input form the user, such as additional search filters etc.

To use this feature a repo will need to extend "JpaSpecificationExecutor"

public interface UserRepository extends JpaRepository<User>, JpaSpecificationExecutor {
      List<T> findAll(Specification<T> spec);
}

The find method can then be called with multiple criteria, or the criteria can be built dynamically based on the situation:

List<User> users = userRepository.findAll(where(userLastNameIs("John")).and(userIsArchived())); 

Alternatively you can also try query by exampe. The idea here is to provide the actual domain object with the populated search fields to an example matcher. Configure the example matcher to control the search and pass it to the findAll method.

Again the repo will need to implement an interface. Check the documentation for the detailed pros/cons of each approach.

Person person = new Person();                          
person.setFirstname("Dave");                           

ExampleMatcher matcher = ExampleMatcher.matching()     
  .withIgnorePaths("lastname")                         
  .withIncludeNullValues()                             
  .withStringMatcherEnding();                          

Example<Person> example = Example.of(person, matcher);
List<Person> people = personRepository.findAll(example);

Upvotes: 5

Petar Petrov
Petar Petrov

Reputation: 596

https://spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa/

This is very good tutorial of Spring Data. I suggest you to start with it. tutorial of Spring Data. If you want to go deeper you can read the documentation.

http://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html

Upvotes: 0

jahra
jahra

Reputation: 1235

You should extend your repository from JpaRepository. Be careful with name of repository (It should follow convention). After you inject your UserRepository bean you will have already implemeted by spring data crud methods like findOne(), findAll(), delete() etc.

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
   //assume your primary key is Long type
}

Also will be useful documentation

Upvotes: 2

Related Questions