我正在尝试创建两个表课程表和游戏表。该课程包含许多游戏。该游戏仅分配给一门课程。 我在春季启动时定义了实体,如下所示:
课程实体:
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 + "]";
// }
}
但是我的课程里总是没有游戏:(course_id总是如图所示) MySql Query
我在我的实体中做错了什么?
答案 0 :(得分:0)
也设置反向关系,即为每个被添加到关系中的游戏设置courseId。
public class Course{
...
public void addGame(Game game) {
courseGames.add(game);
game.setCourse(this);
}
构造函数中的方式相同。