Hello everyone!,
I have a newbie question about hibernate, later of try and try to understand I cannot and then I m here, my question is the following:
I have 3 Classes: Form.java / Question.java / Option.java
in which I formulate the relationship like a survey:
Form has many Questions / Questions has many Options and the bi-directional relationships:
Option has one Question /Questions has one Form /....and in code I have:
Form.java:Code:
@OneToMany(mappedBy="form",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Question> questions = new ArrayList<Question>();
Question.javaCode:
@ManyToOne (cascade = {CascadeType.PERSIST, CascadeType.MERGE})
public Form getForm() {
return form;
}
@OneToMany(mappedBy="question",cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public List<OptionValue> getOptionValues() {
return optionValues;
}
Option.javaCode:
@ManyToOne
public Question getQuestion() {
return question;
}
Later when I try to Create a Form form another form already created... I have the following error:
Object references an unsaved transient instance - save the transient instance before flushing: x.yyy.Question
I don't understand very well the concept of cascade and how this works ...it seems that I want to save one option value before Question ?
Its is the code for create other form from another:
Code:
List questionsToAdd = formManager.getQuestionsById(new Long(idNewForm));
form.setQuestions(questionsToAdd);
//formManager.saveForm(form);
for(int i=0; i<questionsToAdd.size();i++){
Question questionOld = (Question) questionsToAdd.get(i);
Question questionNew = new Question();
questionNew.setLabel(questionOld.getLabel());
questionNew.setTypeQuestion(questionOld.getTypeQuestion());
questionNew.setPosition(questionOld.getPosition());
questionNew.setHasOptionsToAdd(questionOld.getHasOptionsToAdd());
questionNew.setQuestionText(questionOld.getQuestionText());
questionNew.setForm(form);
questionManager.saveQuestion(questionNew);
List optionsToAdd = questionOld.getOptionValues();
for(int j=0; j<optionsToAdd.size();j++){
OptionValue optionValueOld = (OptionValue) optionsToAdd.get(j);
OptionValue optionValueNew = new OptionValue();
optionValueNew.setValueOption(optionValueOld.getValueOption());
optionValueNew.setValue_column(optionValueOld.getValue_column());
optionValueNew.setValue_row(optionValueOld.getValue_row());
optionValueNew.setIsOptionColumn(optionValueOld.isIsOptionColumn());
optionValueNew.setIsOptionRow(optionValueOld.isIsOptionRow());
optionValueNew.setLabel(optionValueOld.getLabel());
optionValueNew.setImage(optionValueOld.getImage());
optionValueNew.setIsFieldOther(optionValueOld.isIsFieldOther());
optionValueNew.setValueFieldOther(optionValueOld.getValueFieldOther());
optionValueNew.setIsActive(optionValueOld.getIsActive());
optionValueNew.setType(optionValueOld.getType());
optionValueNew.setRangeBegin(optionValueOld.getRangeBegin());
optionValueNew.setRangeEnd(optionValueOld.getRangeEnd());
optionValueNew.setRangeStep(optionValueOld.getRangeStep());
optionValueNew.setQuestion(questionNew);
optionManager.saveOptionValue(optionValueNew);
}
}
Well if for example I change the Cascade type to the Entity Option.java where :
Code:
@ManyToOne(cascade = {CascadeType.PERSIST})
public Question getQuestion() {
return question;
}
Something strange that I cannot understand happens... It insert me the FORM , but in the table QUESTION it insert me several times the question, and several options without ID .... something I m making wrong...
Please I will be very greatfull if anyone can help me...
Thanks in advance!