【问题标题】:Parent Entity is not populated in @OneToMany. Hibernate Bidirectional@OneToMany 中未填充父实体。休眠双向
【发布时间】:2018-07-25 11:32:50
【问题描述】:

Hibernate 新手,

我的实体类

@Entity
public class User {

    @Id
    @GeneratedValue//How to restrcit by passing id from response
    @JsonIgnore
    private Integer userId;

    @OneToMany(mappedBy = "user")
    private List<PostEntity> postEntity;

}



@Entity
@Table(name="post")
public class PostEntity {

    @Id
    @GeneratedValue
    @JsonIgnore
    @ApiModelProperty(required=false)
    private Integer id;

    @ManyToOne(fetch=FetchType.LAZY)
    private User user;
}

当我获取 User 时,它的填充 Post 实体也是如此。

URI:http://localhost:8080/jpa/users

[
 {
     "name": "Abdul",
     "birthDate": "2018-07-25T01:29:51.895+0000",
     "postEntity": [
         {
             "description": "My Fourth Post"
         },
         {
             "description": "My First Post"
         },
         {
             "description": "My Second Post"
         }
     ]
 },
 {
     "name": "Anji",
     "birthDate": "2018-07-25T01:29:51.903+0000",
     "postEntity": []
 },
 {
     "name": "Naren",
     "birthDate": "2018-07-25T01:29:51.903+0000",
     "postEntity": []
 }
]

但反过来就不是这样了。当我获取发布其跳过的用户实体时。

URI: localhost:8080/jpa/users/101/posts/11001 回应:

{
    "description": "My First Post"
}

为什么它没有在上面的 JSON 响应中填充用户信息。

获取方法:

用户:

@GetMapping("/jpa/users")
public List<User> retAll(){
    return userRepository.findAll();
}

帖子:

@GetMapping("/jpa/users/{uid}/posts/{pid}")
public Resource<PostEntity> postE(@PathVariable int uid,@PathVariable int pid) {
    Optional<PostEntity> post = postRepository.findById(pid);
    if (!post.isPresent()) {
        throw new UserNotFoundException("POst");
    }

    PostEntity ePost = post.get();
    Resource<PostEntity> resource = new Resource<PostEntity>(ePost);
    return resource;
}

请帮忙。

【问题讨论】:

  • 这取决于您获取的方式。如果您在一次交易中获取它,它将正常工作。
  • 你在 PostEntity 中有getUser 方法吗?
  • 我有合适的 getter 和 setter。不过我还没有发帖。

标签: java spring hibernate spring-boot spring-data-jpa


【解决方案1】:

这实际上是 REST 的预期工作方式。

GET/users:所有用户

GET /users/1 : 用户 1 及其所有孩子的信息

GET/users/1/posts:用户 1 的所有帖子

GET/users/1/posts/10 : 来自用户 1 的帖子 10 及其所有子节点的信息

当您拨打/users/101/posts/11001 时,端点将为您提供来自一位用户(id 101)的一个帖子(id 11001)的信息。

获取父母信息的常用方法有两种:

最快的方法是调用/users 并在前端过滤您想要的帖子。

正确的方法是更改​​帖子的模型 (PostEntity.java) 以包含其“父”User 对象,因此当您对帖子进行 REST 调用时,用户对象被填充。

延伸阅读:

https://softwareengineering.stackexchange.com/questions/274998/nested-rest-urls-and-parent-id-which-is-better-design

也许阅读一些 REST 最佳实践是个好​​主意:

https://hackernoon.com/restful-api-designing-guidelines-the-best-practices-60e1d954e7c9

【讨论】:

  • 很好。如果请求子实体,有什么方法可以填充父实体?
  • @Abdul 更新了我的答案。
  • 那么,你想让我现在从 PostEntity 中删除 @ManyToOne(fetch=FetchType.LAZY) 吗?
  • 我不确定,但我认为这仅在使用 spring-data-rest 和 @RestRepository 注释时才有效,但似乎并非如此。
【解决方案2】:

尝试使用 FetchType

@Entity
public class User {

    @Id
    @GeneratedValue//How to restrcit by passing id from response
    @JsonIgnore
    private Integer userId;

    @OneToMany(mappedBy = "user", fetch=FetchType.EAGER)
    private List<PostEntity> postEntity;

}

注意性能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多