org.springframework.data.mapping.PropertyReferenceException: Роль свойства не найдена для роли типа!
Репозиторий Ролей
import com.synesisit.commonmodule.acl.model.Role;
import org.springframework.data.jpa.repository.JpaRepository;
public interface RoleRepository extends JpaRepository<Role, Integer> {
Role findByRole(String name);
}
РольСервис
@Service
public class RoleService {
private RoleRepository roleRepository;
public Role findByRole(String name){
return roleRepository.findByRole(name);
}
}
РольКонтроллер
@RestController
public class RoleController {
@Autowired
private RoleService roleService;
@PostMapping("/role")
public ResponseEntity<ApiResponse> createRole(@RequestBody @Valid Role role) {
Role existingRole = roleService.findByRole(role.getName());
if (existingRole != null) {
return new ResponseEntity(new ErrorResponse("Role Already Exists ! Please try again..", null),
HttpStatus.BAD_REQUEST);
}
Role createdRole = roleService.save(role);
return ResponseEntity.ok(new ApiResponse(true, "Role Saved Successfully", createdRole));
}
}
Метод в вашем репозитории должен быть findByName
вместо findByRole
, потому что свойство Role равно name
:
public interface RoleRepository extends JpaRepository<Role, Integer> {
Role findByName(String name);
}
Spring Data автоматически генерирует запросы из вашего репозитория на основе имени метода. Итак, findByRole
означает, что Spring хочет сделать запрос к свойству role
. Поскольку его не существует (потому что его имя name
), у вас есть ошибка:
Роль свойства для типа Роль не найдена!