2013-11-09 157 views
0

我想要有一個連接表,它有一個額外的列兩個表之間的ManyToMany關係。我跟着this tutorial,但仍然出現錯誤。我正在使用Spring MVC與休眠 這裏是我的實體:有問題塊和問題,以及它們之間的關聯。 問:休眠ManyToMany與其他列的關係

@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... 

    } 

QuestionBlock:

@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; 


} 

AssociationID:

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方法時,連接表()中沒有插入任何內容。 (只在問題塊表中)

回答

0

我想你忘了設置堅持操作從QuestionBlockAssociationQuestionToQuestionBlock的級聯。

級聯將確保當持久化QuestionBlock實體時,新創建的和關聯的AssociationQuestionToQuestionBlock實體(即處於臨時狀態)也會持久保存。

public class QuestionBlock { 

    ... 

    @OneToMany(mappedBy="questionBlock", cascade = CascadeType.PERSIST) 
    private Set<AssociationQuestionToQuestionBlock> questions; 
0

如果你想保留這個設計,我不會建議,你必須映射添加註釋的AssociationQuestionToQuestionBlockID領域,刪除您多對一協會插入和更新的屬性,並添加MapsId註釋這些關聯。

但是,好的,簡單的,更高效的,更簡單的方法是將AssociationQuestionToQuestionBlock實體視爲所有其他實體,併爲此實體使用自動生成的單列ID。如果您想確保沒有多個具有相同questionID-questionBlockId的實例,請使用唯一的密鑰約束。

順便說一句:爲什麼你使用int爲你的實體的ID,long引用它們在AssociationQuestionToQuestionBlock