0
2つのテーブルコーステーブルとゲームテーブルを作成しようとしています。コースには多くのゲームが含まれています。ゲームは1つのコースにのみ割り当てられます。 私は次のように春のブート時にそれを行うには、エンティティを定義している:2つのテーブル間に1対多の関係があります。スプリングブートJPAが動作しません。
コースエンティティ:
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
私のエンティティで何が間違っていますか?
はい、ありがとうございます。問題は、あなたが言及したように私がこれらのクラスを使用した方法であったエンティティではありませんでした。 – user7413640