1
@Entity public class Organization {

@OneToMany 
List<A> Aobjects;


@OneToMany 
List<B> Bobjects;


@OneToMany 
List<C> Cobjects;

}

I have @OnDelete annotation on all the @OneToMany members. What would be the order of deletion of these objects when the parent object is removed. Is it safe to assume that it would be the order in which the objects are declared.

Sam
  • 8,387
  • 19
  • 62
  • 97
  • What happened when you tried? – Pascal Thivent Nov 10 '10 at 10:59
  • I don't see the SQL logs when you do a cascade delete. But I ran into few issues, if B inturn has a foreign key reference to A which causes A to fail on deletion. Hence the question – Sam Nov 10 '10 at 12:02

1 Answers1

1

I have @OnDelete annotation on all the @OneToMany members. What would be the order of deletion of these objects when the parent object is removed. Is it safe to assume that it would be the order in which the objects are declared.

So you are using a SQL cascade delete on deletion instead of the regular Hibernate mechanism. Since you're using a database mechanism, I'm tempted to say the order in which the objects are declared does not matter (the database is simply not aware of this order) and I would just expect the database to handle things appropriately.

It might influence the order of columns if you are generating the model from annotations though but:

  • I'd be very surprised if this had any impact, that would make everything extremely fragile, unusable.
  • Hibernate/JPA doesn't say anything about the order of columns (because this shouldn't matter, see above).

But I ran into few issues, if B inturn has a foreign key reference to A which causes A to fail on deletion

You should mention that in the question, this is very relevant IMO. Take my answer with a grain of salt but as I said, I'd expect a database engine to handle things appropriately, a database engine has all the required metadata to do it. But I'm not a database expert, I'm not sure what the theory says.

Maybe have a look at questions such as:

Or ask a question about the theory (and the practice for a given engine).

Community
  • 1
  • 1
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124