0

我想在两个表之间建立多对多关系,并带有一个连接表,它有一个额外的列。我按照本教程进行操作,但仍然出现错误。我在 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 方法时,连接表 () 中没有插入任何内容。(仅在问题块表中)

4

2 回答 2

0

我认为您忘记设置从QuestionBlockto的持久操作的级联AssociationQuestionToQuestionBlock

级联将确保在持久化QuestionBlock实体时,新创建的和关联的AssociationQuestionToQuestionBlock实体(处于瞬态)也得到持久化。

public class QuestionBlock {

    ...

    @OneToMany(mappedBy="questionBlock", cascade = CascadeType.PERSIST)
    private Set<AssociationQuestionToQuestionBlock> questions;
于 2013-11-09T10:38:05.867 回答
0

如果您想保留这种设计,我不建议这样做,您必须将映射注释添加到 的字段AssociationQuestionToQuestionBlockID,从您的 ManyToOne 关联中删除可插入和可更新属性,并MapsId为这些关联添加注释。

但好的、简单、更有效、更容易的方法是将AssociationQuestionToQuestionBlock实体视为所有其他实体,并为此实体使用自动生成的单列 ID。如果要确保没有多个实例具有相同的 questionID-questionBlockId,请使用唯一键约束。

顺便说一句:您为什么使用int实体的 ID,并long在 中引用它们AssociationQuestionToQuestionBlock

于 2013-11-09T10:38:33.760 回答