Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Delete only the child object and not the parent object

 
Rithanya Laxmi
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this post and see if it clarifies things for you.

http://www.coderanch.com/forums/posts/list/590146#2690854
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic