Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 781
TS How Avatar asked Sep 07 '16 09:09

TS How


People also ask

Which method is used to fetch all rows in Spring data JPA repository?

findById() method is used for finding the records from mysql.

Which of the below code will fetch all users from UserRepository?

The findAll() method calls userRepository's findAll() method and retrieves all users. The UserRepository extends from the CrudRepository . It provides the type of the entity and of its primary key.

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 does @repository do in Spring?

Spring @Repository annotation is used to indicate that the class provides the mechanism for storage, retrieval, search, update and delete operation on objects.


1 Answers

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

like image 98
jahra Avatar answered Nov 02 '22 07:11

jahra