gpt4 book ai didi

mysql - 两个表之间的一对多关系 Spring boot JPA 不起作用

转载 作者:行者123 更新时间:2023-11-29 19:03:05 26 4
gpt4 key购买 nike

我正在尝试创建两个表“类(class)表”和“游戏表”。该类(class)包含许多游戏。比赛仅分配给一门类(class)。我已经定义了在 Spring Boot 上执行此操作的实体,如下所示:

类(class)实体:

package play_and_learn.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

@Entity
@Table(name = "courses")
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int courseId;
private String courseName;
private String courseDescription;
private String creatorTeacherUsername;

@OneToMany(mappedBy = "course", cascade = CascadeType.ALL)
private List<Game> courseGames;

public Course(String courseName, String courseDescription
, String creatorTeacherUsername, List<Game> courseGames) {
super();
this.courseName = courseName;
this.courseDescription = courseDescription;
this.creatorTeacherUsername = creatorTeacherUsername;
this.courseGames = courseGames;
}

public Course(String courseName, String courseDescription, String creatorTeacherUsername) {
super();
this.courseName = courseName;
this.courseDescription = courseDescription;
this.creatorTeacherUsername = creatorTeacherUsername;
this.courseGames = new ArrayList<Game>();
}

public Course(String courseName, String description) {
super();
this.courseName = courseName;
this.courseDescription = description;
this.creatorTeacherUsername = "";
this.courseGames = new ArrayList<Game>();
}

public Course () {
super();
this.courseName = "";
this.courseDescription = "";
this.courseGames = new ArrayList<>();
}

public void addGame(Game game) {
courseGames.add(game);
}

public int getCourseId() {
return courseId;
}

public void setCourseId(int courseId) {
this.courseId = courseId;
}

public String getCourseName() {
return courseName;
}

public void setCourseName(String courseName) {
this.courseName = courseName;
}

public String getCourseDescription() {
return courseDescription;
}

public void setCourseDescription(String courseDescription) {
this.courseDescription = courseDescription;
}

public String getCreatorTeacherUsername() {
return creatorTeacherUsername;
}

public void setCreatorTeacherUsername(String creatorTeacherUsername) {
this.creatorTeacherUsername = creatorTeacherUsername;
}

public List<Game> getCourseGames() {
return courseGames;
}

public void setCourseGames(List<Game> courseGames) {
this.courseGames = courseGames;
}

public Game getGameByID(int gameID) {
for (Game game : courseGames) {
if (game.getGameId() == gameID) {
return game;
}
}
return null;
}



// @Override
// public String toString() {
// return "Course [courseId=" + courseId + ", courseName=" + courseName
+ ", courseDescription="
// + courseDescription + ", courseGames=" + courseGames + "]";
// }


}

游戏实体

package play_and_learn.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

@Entity
@Table(name = "games")
public class Game {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int gameID;
private String name;
private String description;
private String creatorTeacherUsername;
private String gameType; // MCQ , True & False , etc.

protected int numOfQuestions = 0;

@ManyToOne
@JoinColumn(name = "courseId")
private Course course; // database related field

@OneToMany(mappedBy = "q_id", cascade = CascadeType.ALL)
protected List<Question> questions;

public Game() {
name="";
description="";
creatorTeacherUsername = "";
gameType = "";
numOfQuestions = 0;
questions = new ArrayList<>();
}

public Game(String name, String description, String creatorTeacherUsername
, String gameType, int numnumOfQuestions) {
this.name = name;
this.description = description;
this.creatorTeacherUsername = creatorTeacherUsername;
this.gameType = gameType;
this.numOfQuestions = numnumOfQuestions;
questions = new ArrayList<>();
}


public String getGameType() {
return gameType;
}

public void setGameType(String gameType) {
this.gameType = gameType;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

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

public int getGameId() {
return gameID;
}

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

public String getCreatorTeacherUsername() {
return creatorTeacherUsername;
}

public void setCreatorTeacherUsername(String creatorTeacherUsername) {
this.creatorTeacherUsername = creatorTeacherUsername;
}

public int getNumOfQuestions() {
return numOfQuestions;
}

public void setNumOfQuestions(int numOfQuestions) {
this.numOfQuestions = numOfQuestions;
}

public List<Question> getQuestions() {
return questions;
}

public void setQuestions(List<Question> questions) {
this.questions = questions;
}

public void addQuestion(String qBody, String theRightAnswer, String ... answers) {
Question q = new Question();

q.setqBody(qBody);
q.setAnswer1(answers[0]);
q.setAnswer2(answers[1]);
q.setAnswer3(answers[2]);
q.setAnswer4(answers[3]);
q.setTheRighAnswer(theRightAnswer);

questions.add(q);
}

public void addQuestion(Question question) {
questions.add(question);
}


// @Override
// public String toString() {
// return "Game [gameID=" + gameID + ", name=" + name + ", description=" +
description
// + ", creatorTeacherUsername=" + creatorTeacherUsername + ",
gameType=" + gameType + ", numOfQuestions="
// + numOfQuestions + ", questions=" + questions + "]";
// }


}

但是我的类(class)中总是没有游戏:(如图所示,course_id 始终为 null) MySql Query

我在我的实体中做错了什么?

最佳答案

还设置逆关系,即为添加到关系中的每个游戏设置 courseId。

public class Course{
...
public void addGame(Game game) {
courseGames.add(game);
game.setCourse(this);
}

构造函数中的方法相同。

关于mysql - 两个表之间的一对多关系 Spring boot JPA 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43709007/

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