Spring Boot - Apply a specific query to a database

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Spring Boot - Apply a specific query to a database



I have started with Spring Boot and I have the next doubt.



I am showing in screen all fields of a query to database but I would like to show only the registers on base the next query:


select * from user where remove = false



I only showing the method findAll for each class because this method working ok but I don't get to add a clause where.



This is my method to controller class (UserController.java):


@GetMapping
public List<User> findAll()
return userService.findAll();



This is my method to service class (UserService.java):


List<User> findAll();



This is my method in service implementation class (UserServiceImpl.java):


@Override
public List<User> findAll()
return repository.findAll();



This is my method to repository class (UserRepository.java):


public interface UserRepository extends Repository<User, Integer>

// @Query("select * from user where remove = false")
List<User> findAll();




I have tried to test in the class UserRepository the next annotation:


@Query("select * from user where remove = false")



But by the moment not working ok.



What is the best way to solve it ?, thanks





Have a look at Table 2.3 on docs.spring.io/spring-data/jpa/docs/1.5.0.RC1/reference/html/…
– Selim
Aug 8 at 10:48


Table 2.3




3 Answers
3



Try adding nativeQuery = true in your query


nativeQuery = true


@Query("select * from user where remove = false", nativeQuery = true)





This proposal works perfectly, I just had to change the syntax by: @Query (nativeQuery = true, value = "select * from user where remove = false")
– Eladerezador
Aug 8 at 11:12



Use the query dsl instead native query:


List<User> findByRemoveFalse();



It works equal the native query.



You can use repository query methods, for example:


List<User> findByRemoveFalse();



I think my another answer will be also useful: https://stackoverflow.com/a/51692712/5380322






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS