我想在两个表之间建立多对多关系,并带有一个连接表,它有一个额外的列。我按照本教程进行操作,但仍然出现错误。我在 Hibernate 中使用 Spring MVC 这里是我的实体:有 Questionblocks 和 Questions,以及它们之间的关联。问题:
@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";
}
}
之后,当我尝试调用 Controller 方法时,连接表 () 中没有插入任何内容。(仅在问题块表中)