Remove an Entity from Database using JPA with Hibernate
Maven dependencies used all over JPA tutorials same. Please follow "Setting up Project" section to understand about project configuration.
persistence.xml and other code files reused from How to Persist an Entity in data base using JPA?
Each Sample application from download includes SQL file also.
Remove an Entity :
In order to remove an entity, the entity itself must be managed, it means that it is present in the persistence context. This means that the calling application should have already loaded or accessed the entity. The whole point is before calling EntityManager’s remove(), instance should be in persistent state.
Once an entity in the database, Student student = entintyManager.find(Student.class, 1L); this line will retrieve student entity where identifier value is 1. Then entintyManager.remove(student); removes the entity from database.