Spring JPA - Why Is My Findall Returning Null When There Is Data in My Database

findAll() returns null

Keep

@Repository 
public interface IUserDao extends CrudRepository<User, Integer> {

}

Delete UserRepository class its implementation will be provided by spring-data-jpa.

to access:

@Autowired
private IUserDao repo;

repo.findAll();

JpaRepository .findAll method returns null values and reapeating list via postman

Thank you for your answers, it is appreciated and will give me further angles to keep in mind on future debugging.

The way I solved it in the end was updating the table column names to snake case, i.e. "FirstName" was changed to "first_name". So in hindsight, the mapping was the issue.

JPA Repository.findById() returns null but the value is exist on db

You have to change @RequestBody to @RequestParam. Please update your controller code as below.

    @ResponseBody
@RequestMapping(value="/checkIdDuplicate", method=RequestMethod.POST)
public boolean checkIdDuplicate(@RequestParam String id) {

return memberService.isExistByUserId(id);
}

Spring JPA Query returns Null instead of List

  1. The normal behavior is indeed returning an empty list if no results are found. If a List<Object> is the return value of the method in the defined interface, the method should never return Null.
  2. The problem is that a parameter is given to the method and is not used anywhere in the Query. For some reason Spring decides to return a Null in that case. Solution: remove the unused parameter or use the parameter in the Query.

JPA Repository.findByAll() returns null but everything exists in db

It seems like you are missing @Autowired annotation in the StoreController

@Controller
public class StoragePageController {

@Autowired
private StorageService storageService;

@GET
@RequestMapping(value = "/storage")
public String openAllProductsPage(Model model) {

List<Product> productList = storageService.findAll();
model.addAttribute("productList", productList);
return "storage";
}
}

OR

The most recommended way to the dependency injection is to create a constructor with all dependencies.

@Controller
public class StoragePageController {


private final StorageService storageService;

public StoragePageController(StorageService storageService) {
this.storageService = storageService;
}

@GET
@RequestMapping(value = "/storage")
public String openAllProductsPage(Model model) {

List<Product> productList = storageService.findAll();
model.addAttribute("productList", productList);
return "storage";
}
}


Related Topics



Leave a reply



Submit