java - Java Spring 中使用 @OneToMany 关系发生奇怪的开始

标签 java spring hibernate

我的代码中有一个奇怪的错误,我不知道为什么会发生这种情况(制作 REST API)。

我有一个乐谱表和一个评论表。乐谱可以有来自不同用户的多个评论。因此,sheetmusic 类包含一个注释列表。

当我尝试通过 id 获取乐谱时,它会返回乐谱和评论(这很好)。但每条评论也都有乐谱。该乐谱又具有评论,该评论又具有乐谱。我想你已经知道接下来会发生什么了;)。

乐谱应该只包含所有评论,评论不能包含乐谱,否则会被拦截。

Screenshot of a single sheetmusic

所以我使用了一些类:

评论 Controller

package server.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.entity.Comment;
import server.entity.SheetMusic;
import server.repository.CommentRepository;
import server.repository.SheetMusicRepository;

import java.util.List;
import java.util.Map;

@RestController
public class CommentController {

    @Autowired
    CommentRepository commentRepository;

    @Autowired
    SheetMusicRepository sheetMusicRepository;

    @PostMapping("/comments")
    public Comment create(@RequestBody Map<String,String> body){
        int sheetId = Integer.parseInt(body.get("sheetId"));
        int userId = Integer.parseInt(body.get("userId"));
        String title = body.get("title");
        String description = body.get("description");
        int score = Integer.parseInt(body.get("score"));

        SheetMusic sheetMusic = sheetMusicRepository.findById(sheetId).orElse(null);

        Comment comment = new Comment(sheetMusic,userId,title,description,score);
        return commentRepository.save(comment);
    }
}

评论类

package server.entity;

import javax.persistence.*;

@Entity
@Table(name = "comment")
public class Comment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "user_id")
    private int userId;

//    @ManyToOne
//    private User user;
//
//    public int getUsername(){
//        return user.getUsername();
//    }

    private String title;

    private String description;

    private int score;

    // Een comment hoort maar bij 1 sheetmusic
    // Relatie op basis van het sheet_music_id
    @ManyToOne
    @JoinColumn(name="sheet_music_id", nullable = false)
    private SheetMusic sheetMusic;

    public Comment() {
    }

    public Comment(SheetMusic sheetMusic, int userId, String title, String description, int score) {
        this.sheetMusic = sheetMusic;
        this.userId = userId;
        this.title = title;
        this.description = description;
        this.score = score;
    }

    public int getId() {
        return id;
    }


    // Dit laat de sheet music id zien in json
    public int getSheet_music_id(){
        return this.sheetMusic.getId();
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

}
package server.entity;

import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name="sheet_music")
public class SheetMusic {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    private String title;

    private String componist;

    private String key;

    private String instrument;

    // Een sheetmusic heeft meerdere comments
    // mappedBy = "sheetMusic" is de variabele naam in de Comment entity
    @OneToMany(mappedBy = "sheetMusic")
    List<Comment> comments = new ArrayList<>();

    @Lob
    @Type(type="org.hibernate.type.BinaryType")
    private byte[] pdf;

    public SheetMusic() {
    }

    public SheetMusic(String title, String componist, String key, String instrument, byte[] pdf) {
        this.title = title;
        this.componist = componist;
        this.key = key;
        this.instrument = instrument;
        this.pdf = pdf;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getComponist() {
        return componist;
    }

    public void setComponist(String componist) {
        this.componist = componist;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getInstrument() {
        return instrument;
    }

    public void setInstrument(String instrument) {
        this.instrument = instrument;
    }

    public byte[] getPdf() {
        return pdf;
    }

    public void setPdf(byte[] pdf) {
        this.pdf = pdf;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }
}

最佳答案

您应该删除以下代码:

@JoinColumn(name="sheet_music_id", nullable = false).

关于java - Java Spring 中使用 @OneToMany 关系发生奇怪的开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60792996/

相关文章:

java - 我需要将一个对象转换为字符串,然后通过将其转换回我的对象​​来检索它

java - 在哪里锁定以进行实体行的原子获取或创建

java - 这段简单的 Java 代码有什么问题?

java - Spring aop不止一种方法

spring - 在OSGi bundle 中访问Spring上下文

java - 在Spring Boot测试中未加载Gradle测试依赖项

java - 使用多线程的JPA持久化

java - 从另一个对象列表创建对象元素的列表

java - 如何获取系统数据并使我的应用程序针对操作系统独一无二

java - 向JPA中的实体添加约束外键