From the course: Java Persistence with JPA and Hibernate

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Removing an entity instance

Removing an entity instance

- [Instructor] In JDBC, among the other CRUD operations, you would also need to perform the DELETE operation. Using ORM, or in other words, Hibernate, is this possible? Well, it's possible, but the concept of removal in ORM is a little bit different to JDBC. In the entity manager, there's a method named Remove. In the code for this use case, you can see that there's a find for a book with an ID of one. If it's not already in the context, it will be brought from the database. If not, the existing instance in the context will be used. So let's call the Remove method to remove book1. So below book1 here, we call em.remove and parse in book1. What really happens here is that the instance book1 will be marked for removal, but it will remain within the context. At the end of the transaction, Hibernate will look at the context and see what changes have happened. It will see that entity instance book1 has been marked for removal and therefore, it shows a delete query on the book database…

Contents