gpt4 book ai didi

java - Spring 数据 REST findBy 嵌套实体

转载 作者:搜寻专家 更新时间:2023-10-31 19:31:57 24 4
gpt4 key购买 nike

我有一个使用 Spring-data-rest 生成我的 REST 接口(interface)的 Spring Boot 项目,我正在尝试允许对嵌套资源进行分页。我跟着这个workaround并陷入了实现 findBy 查询的困境。

我有以下设备实体:

@Entity
@Table(name = "devices")
public class Device extends ResourceSupport {

...

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;
}

我需要使用 userId 查询它:

@RequestMapping(path = "/users/{id}/devices", method = RequestMethod.GET)
public ModelAndView getUserDevices(@PathVariable Integer id) {
return new ModelAndView("forward:/devices/search/findByUserId?userId=" + id);
}

所以我在我的 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);

}

但是在尝试向此端点发送请求时出现以下异常:

Unable to locate Attribute  with the the given name [id] on this ManagedType [com.example.User]

有人对我做错了什么有什么建议吗?

编辑:感谢您的回复,我将方法更改为

Page<Device> findByUser_UserId(@Param("user") User user, Pageable pageable);

我的用户实体包含一个 userId 字段。

但是,现在我遇到以下异常:

{
"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!"
}

所以我似乎错误地使用了查询?

最佳答案

请看docs .

Property expressions can refer only to a direct property of the managed entity, as shown in the preceding example. At query creation time you already make sure that the parsed property is a property of the managed domain class. However, you can also define constraints by traversing nested properties. Assume a Person has an Address with a ZipCode. In that case a method name of

List<Person> findByAddressZipCode(ZipCode zipCode);

[...] Although this should work for most cases, it is possible for the algorithm to select the wrong property. Suppose the Person class has an addressZip property as well. The algorithm would match in the first split round already and essentially choose the wrong property and finally fail (as the type of addressZip probably has no code property). To resolve this ambiguity you can use _ inside your method name to manually define traversal points. So our method name would end up like so:

List<Person> findByAddress_ZipCode(ZipCode zipCode);

这意味着:您所做的应该有效,除非您的 Device 类有一个字段 userId

您向我们展示的异常是原始问题中的以下内容。

Unable to locate Attribute with the the given name [id] on this ManagedType [com.example.User]

这看起来您的类 User 没有名为 id 的字段。由于您没有向我们展示此类的代码,我无法判断是否是这种情况。 (确保您在存储库中导入了正确的 User 类)

问题已更新,因此另一个答案存在另一个错误

现在的问题是您将 User 对象传递给您的存储库方法,但按 id 进行过滤。只需将第一个参数更改为用户 ID 的数据类型。我认为这应该有效。

假设 id 的类型是 int 方法可能如下所示

Page<Device> findByUser_UserId(@Param("userId") int userId, Pageable pageable);

关于java - Spring 数据 REST findBy 嵌套实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47809638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com