I have a Spring Boot project using Spring-data-rest for generating my REST interface and I am trying to allow pagination for nested resources. I followed this workaround and got stuck with implementing the findBy query.
I have the following Device entity:
@Entity
@Table(name = "devices")
public class Device extends ResourceSupport {
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
}
I need to query it using userId:
@RequestMapping(path = "/users/{id}/devices", method = RequestMethod.GET)
public ModelAndView getUserDevices(@PathVariable Integer id) {
return new ModelAndView("forward:/devices/search/findByUserId?userId=" + id);
}
So I created the following method in my DeviceRepository:
@Repository
public interface DeviceRepository extends JpaRepository<Device, Long> {
@PreAuthorize("hasRole('ROLE_ADMIN') OR @securityCheck.check(#user, authentication)")
Page<Device> findByUserId(@Param("user") User user, Pageable pageable);
}
But I am getting the following exception when trying to send request to this endpoint:
Unable to locate Attribute with the the given name [id] on this ManagedType [com.example.User]
Does anyone have some suggestion what I am doing wrong?
EDIT: Thank you for your responses, I changed the method to
Page<Device> findByUser_UserId(@Param("user") User user, Pageable pageable);
My user entity contains a userId
field.
However, now I am getting the following exception:
{
"cause": {
"cause": {
"cause": null,
"message": "Cannot resolve URI 1. Is it local or remote? Only local URIs are resolvable."
},
"message": "Failed to convert from type [java.net.URI] to type [@org.springframework.data.repository.query.Param com.example.User] for value '1'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI 1. Is it local or remote? Only local URIs are resolvable."
},
"message": "Failed to convert 1 into me.originalsin.user.model.User!"
}
So it seems I am using the query wrongly?