levg wrote:
Hibernate version: 3.0
Just a simple question. I'd like to delete a parent entity together with its child records. The cascade property in the entity hbm file is set to "all-delete-orphan".
When I use
session.delete(session.load(entity.getClass(), entity.getId()));
everything works fine and child records are deleted.
But my motivation is to be able to delete the whole thing without loading the entity. The following code
session.createQuery("delete Entity where id = 1").executeUpdate();
throws a "child record found" exception.
What can the problem be? Please your advice. Thanks a lot in advance.
L G
In hibernate3 the team decided to follow the ejb3 spec and change the function of session.delete around entirely. It will only delete the object and all its children if EVERYTHING about an entity and its children is already loaded into memory from the database. This is to avoid performance problems involved with what you explained, loading everything into memory to then just delete it all.
The best way to do it now (performance wise) is to run
Code:
session.update("delete...").executeUpdate()
queries with some custom written hql delete queries.