Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringDataJPA:如何获取特定类型的所有实体以及每个实体';s关联实体?_Spring_Spring Data Jpa - Fatal编程技术网

SpringDataJPA:如何获取特定类型的所有实体以及每个实体';s关联实体?

SpringDataJPA:如何获取特定类型的所有实体以及每个实体';s关联实体?,spring,spring-data-jpa,Spring,Spring Data Jpa,我有一个Post实体 @Entity public class Post { @Id private UUID id; @NotNull private String title; @NotNull private String content; @NotNull private String identifier; @NotNull private String category; @NotN

我有一个
Post
实体

@Entity
public class Post {

    @Id
    private UUID id;

    @NotNull
    private String title;

    @NotNull
    private String content;

    @NotNull
    private  String identifier;

    @NotNull
    private String category;

    @NotNull
    @Column(name = "created_at")
    private Date createdAt;

    @NotNull
    @Column(name = "updated_at")
    private Date updatedAt;

    public Post (){

    }

    public Post (String title, String content, String category){
        this.title = title;
        this.content = content;
        this.category = category;
    }

    // rest of the getters and setters

}
这是我的
注释
实体:

@Entity
public class Comment {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private UUID id;

    @NotNull
    private String name;

    @NotNull
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer identifier;

    @NotNull
    private String email;

    @NotNull
    private String content;

    @NotNull
    @ManyToOne
    @JoinColumn(name = "post_id")
    private Post postId;

    @NotNull
    @Column(name = "created_at")
    private Date createdAt;

    public Comment() {

    }

    public Comment(String name, String email, String content){
        this.name = name;
        this.email = email;
        this.content = content;
    }
}
这是我的邮政管理员:

@RestController
@RequestMapping("/posts")
public class PostController {

    private String getIdentifier(String str){
        return String.join("-", str.split(" "));
    }

    @Autowired
    private PostService postService;

    @RequestMapping(value = "", method = {GET, HEAD})
    public List<Post> getAllPosts(){
        return postService.getAllPosts();
    }

    @RequestMapping(value = "", method = {POST, OPTIONS})
    public Post addNewPost(@RequestBody Post post){
        post.setId(UUID.randomUUID());
        post.setIdentifier(this.getIdentifier(post.getTitle()));
        post.setCreatedAt(new Date());
        post.setUpdatedAt(new Date());
        return postService.savePost(post);
    }

    @RequestMapping(value = "/{id}", method = {GET, HEAD})
    public Post getOnePost(@PathVariable UUID id){
        return postService.getOne(id);
    }

    @RequestMapping(value = "/{id}", method = DELETE)
    public void deleteOnePost(@PathVariable UUID id){
        postService.deleteOnePost(id);
    }
}
@RestController
@请求映射(“/posts”)
公共类后置控制器{
私有字符串getIdentifier(字符串str){
返回字符串.join(“-”,str.split(”);
}
@自动连线
私人邮政服务;
@RequestMapping(value=”“,method={GET,HEAD})
公共列表getAllPosts(){
return postService.getAllPosts();
}
@RequestMapping(value=”“,method={POST,OPTIONS})
公共帖子addNewPost(@RequestBody Post){
post.setId(UUID.randomuid());
setIdentifier(this.getIdentifier(post.getTitle());
post.setCreatedAt(新日期());
post.setUpdatedAt(新日期());
返回邮政服务。保存邮政(post);
}
@RequestMapping(value=“/{id}”,method={GET,HEAD})
public Post getOnePost(@PathVariable UUID id){
返回邮政服务。getOne(id);
}
@RequestMapping(value=“/{id}”,method=DELETE)
public void deleteOnePost(@PathVariable UUID id){
postService.deleteOnePost(id);
}
}
我的问题是,每当我获取所有帖子时,如何获取每个帖子的所有评论


对不起,我来自NoSQL的背景,所以一开始这有点令人生畏。

您需要做的是创建一个双向的
@OneToMany
关联,从
Post
Comments

Post
类中添加字段

@OneToMany(
        mappedBy = "postId", 
        cascade = CascadeType.ALL
    )
private List<Comments> comments = new ArrayList<>();
@OneToMany(
mappedBy=“postId”,
cascade=CascadeType.ALL
)
私有列表注释=新建ArrayList();

从现在起,当您从数据库中获取
Post
时,将同时获取
评论。

嘿,感谢您的回答。使用泛型,
Post comment
,只是为了举例,还是我遗漏了一些我应该知道的东西?