-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mistakes in Relationships -
PostPosted: Fri Jun 19, 2009 12:13 pm 
Newbie

Joined: Mon Mar 30, 2009 6:18 pm
Posts: 2
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.java
Code:
  @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
Code:
@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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.