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 in code I have:

Form.java:
   @OneToMany(mappedBy="form",cascade = CascadeType.ALL, fetch =
FetchType.LAZY)
     private List<Question> questions = new ArrayList<Question>();

Question.java
  @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.java
 @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:

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

                         }


        }


Please I will be very greatfull if anyone can help me...
Thanks in advance!



-- 
Francisco
 Student Informatikingenieur
------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
hibernate-devel mailing list
hibernate-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to