wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Delete only the child object and not the parent object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Delete only the child object and not the parent object " Watch "Delete only the child object and not the parent object " New topic
Author

Delete only the child object and not the parent object

Rithanya Laxmi
Ranch Hand

Joined: Jan 24, 2011
Posts: 136
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

Joined: Mar 07, 2010
Posts: 1648
    
    7

Have a look at this post and see if it clarifies things for you.

http://www.coderanch.com/forums/posts/list/590146#2690854


[How To Ask Questions][Read before you PM me]
 
Consider Paul's rocket mass heater.
 
subject: Delete only the child object and not the parent object