-->
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.  [ 2 posts ] 
Author Message
 Post subject: Many to one Transient probleme
PostPosted: Sat Jun 20, 2009 12:21 am 
Newbie

Joined: Mon Mar 30, 2009 6:18 pm
Posts: 2
Servus!

Ich habe eine frage über Hibernate, etwas Ich mache nicht gut... ich weisses nicht die probleme...

Ich habe 3 classes: Form.java / Question.java / Option.java
Die relationen ist : Form has many Questions / Questions has many Options ....und die code ist:

Code:
Form.java:
   @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;
    }


Später wenn bin ich erfärung ein "Form" auf "Alte Form" Ich habe :

Object references an unsaved transient instance - save the transient instance before flushing: x.yyy.Question
Ich weißes nicht genau die probleme aber Ich denke die probleme is wenn ich möchte "save" ein Option vor Question...

Die code ist:
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);
     
                         }
         
           
        }


Bitte ...Ich werde sehr glucklich sein ob jemmand kann hilf mir
Danke


Top
 Profile  
 
 Post subject: Re: Many to one Transient probleme
PostPosted: Fri Jun 26, 2009 4:36 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Du musst beim Bearbeiten von Listen beiden "Enden" der Beziehung passend verdrahten. Z.b. so:

Question
Code:
public void addOption(Option option){
     option.setQuestion(this);
     this.options.add(option);
}

public void removeOption(Option option){
    option.setQuestion(null);
    this.options.remove(option);
}


Und dann später nur noch em.saveOrUpdate(question) aufrufen.


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

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.