0

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

Rahmat Effendi
  • 337
  • 2
  • 11
  • 29
  • 1
    Which line is line 89 as given in the error? – RoToRa May 27 '22 at 11:24
  • As the previous commenter said, we need to know lines enumeration to detect the error because there is 89th line mentioned in the error stack trace. It is probable that your id (which you put to the methods getRoleById and findPermissionByRoleId) is null. – Evgenia Rubanova May 28 '22 at 11:43
  • @EvgeniaRubanova I don't think it is possible for `id` to be `null` here. It's a required path variable. The route would not match if it were missing or not a number in the URL. – RoToRa May 30 '22 at 14:21

2 Answers2

0

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

Gurujegan
  • 11
  • 3
0

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);
Rafael da Silva
  • 302
  • 2
  • 13