简体   繁体   中英

Spring Data JPA: How to fetch all entities of a specific type along with each entity's associated entities?

I have a Post entity

@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

}

And this is my Comment entity:

@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;
    }
}

And this is my post controller:

@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);
    }
}

My question is how do I fetch all the comments for each individual post, whenever I fetch all the posts?

Sorry, I come from a NoSQL background, so this is a bit daunting at first.

What you need to do is to create a bidirectional @OneToMany association from the Post to Comments :

add a field in Post class

@OneToMany(
        mappedBy = "postId", 
        cascade = CascadeType.ALL
    )
private List<Comments> comments = new ArrayList<>();

From now on, when you get Post from the database, Comments will be fetched at the same time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM