gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:51:13 24 4
gpt4 key购买 nike

我的代码中有一个奇怪的错误,我不知道为什么会发生这种情况(制作 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/

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