This discussion is archived
2 Replies Latest reply: Mar 25, 2013 6:31 AM by cdelahun RSS

Delete only the child object and not the parent object

800839 Newbie
Currently Being Moderated
Hi,

I have the below code:-


TAnswer
----------
@ManyToOne(fetch = FetchType.LAZY)
     @JoinColumn(name = "question_id", nullable = false)
//     @Cascade(value=CascadeType.ALL) 
     public TQuestion getTQuestion() {
          return this.TQuestion;
     }
     TQuestion
----------
@OneToMany(fetch = FetchType.LAZY, mappedBy = "TQuestion", orphanRemoval = true)
     @Cascade(value=CascadeType.ALL)
     /*@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
            org.hibernate.annotations.CascadeType.DELETE,
            org.hibernate.annotations.CascadeType.MERGE,
            org.hibernate.annotations.CascadeType.PERSIST,
            org.hibernate.annotations.CascadeType.DELETE_ORPHAN})*/
     public List<TAnswer> getTAnswers() {
          return this.TAnswers;
     }
     
     
     In Java:-
     
     
     public void removeAnswers(TQuestion question)
                    throws ApplicationException {
               List<TAnswer> answerList =  question.getTAnswers();
               deleteall(answerList);
     }
     
     public void deleteall(Collection objects) {
     
               try {
                    getHibernateTemplate().deleteAll(objects);
                    getHibernateTemplate().flush();
               } catch (Exception e) {
                    throw new ServerSystemException(ErrorConstants.DATA_LAYER_ERR, e);
               }
     }
               Here the "deleteall" will delete both the answer records and question records, I don't want the questions
     records to be deleted. I have tried making the question as null when we set the answer object to be passed for delete
     bit still it is     deleting the question records as well.How to achieve the above in deleting only the answer (child) records
     and not the question(parent) record? Is there any thing we need to do with @Cascade for Question object? Please clarify.
     
Thanks.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points