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

Delete only the child object and not the parent object

Rithanya Laxmi
Ranch Hand

Joined: Jan 24, 2011
Posts: 136

I have the below code:-

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "question_id", nullable = false)
// @Cascade(value=CascadeType.ALL)
public TQuestion getTQuestion() {
return this.TQuestion;


@OneToMany(fetch = FetchType.LAZY, mappedBy = "TQuestion", orphanRemoval = true)
public List<TAnswer> getTAnswers() {
return this.TAnswers;

In Java:-

public void removeAnswers(TQuestion question)
throws ApplicationException {
List<TAnswer> answerList = question.getTAnswers();

public void deleteall(Collection objects) {

try {
} 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.

Bill Gorder

Joined: Mar 07, 2010
Posts: 1648

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


[How To Ask Questions][Read before you PM me]
I agree. Here's the link: http://aspose.com/file-tools
subject: Delete only the child object and not the parent object
Similar Threads
Can't remove (delete) rows in one to many relationship
Hibernate 3.3.1 - Delete doesn't work
Composite like relationship - Mapping Question
@PrimaryKeyJoinColumn with bidirectional @OneToOne relationship and @Id
hibernate delete performance