Home > OS >  Get NULL in Where Condition in Spring Boot
Get NULL in Where Condition in Spring Boot

Time:05-27

I'm new in Java Language and now I have a small project, but I'm stuck. I want to select data from Permission table with where option.

I have to create query in PermissionRepository like this

@Repository
public interface PermissionRepository extends JpaRepository<Permission, Long> {
    @Query(value = "SELECT * FROM permissions WHERE role_id = ?1", nativeQuery = true)
    List<Permission> findPermissionByRoleId(Long role_id);
}

And I call it in the controller like this

@GetMapping(value = "/secure/roles/edit/{id}")
public ModelAndView editRole(@PathVariable(value = "id") Long id, @Valid Role role) {
    ModelAndView modelAndView = new ModelAndView();
        
    try {
        Role roles = roleService.getRoleById(id);
        List<Permission> p = permissionRepository.findPermissionByRoleId(id);
        System.out.println(p);
            
        modelAndView.setViewName("/secure/master/role/edit");
    } catch (MessageNotFoundExeptions e) {
        e.printStackTrace();
        modelAndView.addObject("message", e.getMessage());
        modelAndView.setViewName("/secure/master/role/edit");
    }
      return modelAndView;
}

I got a null in my response : enter image description here

Anyone can help me fix this error? Thanks

CodePudding user response:

I think you need to verify the argument (@Valid Role role) or remove it and try again

CodePudding user response:

to know better how to help you, we need to know what line 89 is, but I believe the error is in the variable 'id', so you can try the following:

change the method signature to:

@GetMapping(value = "/secure/roles/edit/{id}")
public ModelAndView editRole(@PathVariable Long id, @Valid Role role)

or

@GetMapping(value = "/secure/roles/edit/{id}")
public ModelAndView editRole(@PathVariable("id") Long id, @Valid Role role)

You can see more practical examples in the link: https://www.baeldung.com/spring-pathvariable

the repository I would do as follows:

@Query(value = "SELECT * FROM permissions WHERE role_id = :role_id", nativeQuery = true)
List<Permission> findPermissionByRoleId(Long role_id);
  • Related