追加の列を持つ結合テーブルを使用して、2 つのテーブル間に ManyToMany 関係を持たせたいと考えています。このチュートリアルに従いましたが、それでもエラーが発生します。私は Hibernate で Spring MVC を使用しています。私のエンティティは次のとおりです。クエスチョンブロックとクエスチョン、およびそれらの間の関連付けがあります。質問:
@Entity
@Table(name = "Question")
public class Question {
@Id
@GeneratedValue
int ID;
String title;
String description;
String Type;
String defaultValue;
boolean deleted;
boolean isDraft;
boolean visible;
Timestamp modifiedDate;
String modifiedBy;
@OneToMany(mappedBy = "question",fetch=FetchType.EAGER)
private Set<AssociationQuestionToQuestionBlock> questionBlocks;
//GETTERS, SETTERS, CONSTRUCTORS...
}
質問ブロック:
@Entity
@Table(name = "Questionblock")
public class QuestionBlock {
@Id
@GeneratedValue
int ID;
String title;
String description;
boolean deleted;
boolean isDraft;
boolean visible;
Timestamp modifiedDate;
String modifiedBy;
@OneToMany(mappedBy="questionBlock")
private Set<AssociationQuestionToQuestionBlock> questions;
@ManyToMany(mappedBy="questionBlocks")
Set<Survey> surveys;
public void addQuestion(Question q, int pos){
AssociationQuestionToQuestionBlock association = new AssociationQuestionToQuestionBlock();
association.setQuestion(q);
association.setQuestionBlock(this);
association.setQuestionID(q.getID());
association.setQuestionBlockID(this.getID());
association.setPosition(pos);
if(this.questions==null){
Set<AssociationQuestionToQuestionBlock> questionBlocks = new HashSet<>();
questionBlocks.add(association);
this.questions=questionBlocks;
}else{
this.questions.add(association);
}
if(q.getQuestionBlocks()==null){
Set<AssociationQuestionToQuestionBlock> questionBlocks = new HashSet<>();
questionBlocks.add(association);
q.setQuestionBlocks(questionBlocks);
}else{
q.getQuestionBlocks().add(association);
}
}
}
協会
@Entity
@Table(name = "QuestionBlock_Question")
@IdClass(AssociationQuestionToQuestionBlockID.class)
public class AssociationQuestionToQuestionBlock {
@Id
private long QuestionBlockID;
@Id
private long QuestionID;
int Position;
@ManyToOne
@JoinColumn(name = "QuestionBlockID", updatable = false, insertable = false, referencedColumnName = "ID")
private QuestionBlock questionBlock;
@ManyToOne
@JoinColumn(name = "QuestionID", updatable = false, insertable = false, referencedColumnName = "ID")
private Question question;
}
アソシエーション ID:
public class AssociationQuestionToQuestionBlockID implements Serializable {
private long QuestionID;
private long QuestionBlockID;
public int hashCode() {
return (int) (QuestionID + QuestionBlockID);
}
public boolean equals(Object object) {
if (object instanceof AssociationQuestionToQuestionBlockID) {
AssociationQuestionToQuestionBlockID otherId = (AssociationQuestionToQuestionBlockID) object;
return (otherId.QuestionID == this.QuestionID)
&& (otherId.QuestionBlockID == this.QuestionBlockID);
}
return false;
}
}
これが私のコントローラーです:
@Controller
@RequestMapping("/")
public class TestingController {
SurveyService surveyService;
@RequestMapping("test")
public String test(Model model) {
Question q = surveyService.getQuestion(7);
QuestionBlock qb = new QuestionBlock("tesztblokk", "ee", "czadam");
qb.addQuestion(q,1);
surveyService.save(qb);
return "home";
}
}
その後、コントローラーメソッドを呼び出そうとすると、結合テーブルに何も挿入されません ()。(質問ブロック表のみ)