Many-to-Many relationship problem

when i add a collection to one side of the many-to-many relationship it doesn't add it to the other side.

ex. A test has many multiple choice questions, and multiple choice question can belong to many tests(makes creating a final exam easier, so you can use old questions)

relevant tables:

test(id, testName, courseid)

test_mcquestion(testid, mcquestionid) - both foreign keys to respective tables

mcquestion(id, question, optionA....optionE, answer)

here are the entity beans that netbeans created, i think there is something wrong with the many-to-many mapping...

@Entity

@Table(name ="test")

publicclass Testimplements Serializable{

@Id

@Column(name ="id", nullable =false)

private Integer id;

@Column(name ="testName", nullable =false)

private String testName;

@ManyToMany(mappedBy ="testidCollection")

private Collection<Essay> essayidCollection;

@ManyToMany(mappedBy ="testidCollection", fetch=FetchType.EAGER)

private Collection<Mcquestion> mcquestionidCollection;

@ManyToMany(mappedBy ="testidCollection")

private Collection<Truefalse> truefalseidCollection;

@JoinColumn(name ="courseID", referencedColumnName ="id")

@ManyToOne

private Course courseID;

...appropriate setters & getters....

@Entity

@Table(name ="mcquestion")

publicclass Mcquestionimplements Serializable{

@Id

@Column(name ="id", nullable =false)

private Integer id;

@Column(name ="question", nullable =false)

private String question;

@Column(name ="optionA", nullable =false)

private String optionA;

@Column(name ="optionB", nullable =false)

private String optionB;

@Column(name ="optionC", nullable =false)

private String optionC;

@Column(name ="optionD", nullable =false)

private String optionD;

@Column(name ="optionE", nullable =false)

private String optionE;

@Column(name ="answer", nullable =false)

private String answer;

@JoinTable(name ="test_mcquestion", joinColumns ={

@JoinColumn(name ="mcquestionid", referencedColumnName ="id")

}, inverseJoinColumns ={

@JoinColumn(name ="testid", referencedColumnName ="id")

})

@ManyToMany

private Collection<Test> testidCollection;

...appropriate setters & getters....

code to create mcquestion.....

Mcquestion newmc =new Mcquestion();

newmc.setQuestion(request.getParameter("question"));

newmc.setOptionA(request.getParameter("optionA"));

newmc.setOptionB(request.getParameter("optionB"));

newmc.setOptionC(request.getParameter("optionC"));

newmc.setOptionD(request.getParameter("optionD"));

if(request.getParameter("optionE") !=null){

newmc.setOptionE(request.getParameter("optionE"));

}

ArrayList<Test> newcoll =new ArrayList<Test>();

newcoll.add(testFacade.find(Integer.parseInt(request.getParameter("testID"))));

newmc.setTestidCollection(newcoll);

newmc.setAnswer(request.getParameter("answer"));

mcquestionFacade.create(newmc);

any help would be greatly appreciated

[5438 byte] By [boots54a] at [2007-11-27 5:18:29]
# 1
Yes. It's the normal behaviour. It's responsibility of the programmer fill both sides of the relationship.hayken
haykena at 2007-7-12 10:41:38 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...