1

If i want to remove an entity with hibernate i get the message Removing a detached instance.... I understand that's because my transaction/session is closed.

Currently how i do it: If i lookup an object, i open a transaction, do the lookup, commit, close the transaction. The same for the deletion. Whats the right way to do the deletion? Do i need to keep my transaction open all the time (deletion is executed some time later as the lookup). Or do i need a lookup again for the deletion?

2 Answers 2

1

You have 2 options:

Keep the session open longer so the same session that loaded the object can delete it - this really depends on the architecture of your application.

Alternative re-load the object just before your delete call.

Sign up to request clarification or add additional context in comments.

Comments

1

You can also create and execute a DELETE query with a WHERE clause passing the values of the primary key fields. In that case, you don't have to re-attach your entity.

4 Comments

Do i lose any hibernate functionality if if do the deletion "by hand". E.g. the "cascade" annotation -> Do children get deleted if i do it with a query?
Cascading is not working in that case, you'd have to control it manually.
@eternay: Cascading delete can also be specified at the DDL level.
Of course, but we were talking about "hibernate functionality".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.