Hibernate:How to delete in cascade?

It appears you are creating a detached entity and trying to delete it, rather than fetching a complete entity from the DB. In this case, even if you have cascades defined, they won't work because and relationship of the User entity is not populated.

It appears you are creating a detached entity and trying to delete it, rather than fetching a complete entity from the DB. In this case, even if you have cascades defined, they won't work because and relationship of the User entity is not populated. If the email is the primary key, then you should do the following: User user = session.

Load(User. Class, "foo@bar. Com"); session.

Delete(user) Even with that, if any other entity references a User, it will logically fail - the just look at the database constraints. You have multiple ways to fix this: map List in your user, thus making the relationship bi-directional. Thus you'll be able to set the cascade on the User side as well before deleting a user find (by a query) all packages that belong to him, and delete them.

If the package does not have any other cascades, use an HQL query: DELETE FROM Package WHERE user=:user".

Bozho It doesn't work. Keeps saying : 17:36:08 WARN JDBCExceptionReporter SQL Error: 1451, SQLState: 23000 17:36:08 ERROR JDBCExceptionReporter Cannot delete or update a parent row: a foreign key constraint fails (test. Package, CONSTRAINT FK243B5492B92BA2 FOREIGN KEY (user_email) REFERENCES user (email)) 17:36:08 ERROR AbstractFlushingEventListener Could not synchronize database state with session – Valter Henrique Mar 1 at 20:40 @Valter Henrique as I said, show the whole user entity.

And entities that refer to it. – Bozho Mar 1 at 20:46 @Bozho sorry by that, I edit now. Thanks by your attention friend.

– Valter Henrique Mar 1 at 21:06 @Valter Henrique see the updated answer – Bozho Mar 1 at 21:12 @Bozho still doesn't work – Valter Henrique Mar 1 at 21:23.

Try to update your package class also, like private static void deleteUser(Package package) { Session session = new getSession(); session. BeginTransaction(); try{ User usuario = (Usuario) session. Load(Usuario.

Class, "valter@gmail. Com"); package. SetUser(null); session.

Merge(package); session. Delete(usuario); session.getTransaction().commit(); }catch(bernateException he){ session.getTransaction().rollback(); }finally{ session.close(); }}.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions