From the course: Java Persistence with JPA and Hibernate

Unlock the full course today

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

Implementing the repository pattern

Implementing the repository pattern - JPA Tutorial

From the course: Java Persistence with JPA and Hibernate

Implementing the repository pattern

- [Instructor] Let's implement the repository pattern for the book entity. Make sure you run the mariadb.init.sql script to clean up the database and then run the mariadb-08_02.sql script. I've defined an interface named BookRepository in the package repositorypattern. In it, there are methods for adding a book, removing a book, updating, and querying for books. You can then create a class that implements this BookRepository interface. The BookRepositoryImpl class implements the BookRepository interface. It defines an EntityManager and the constructor initializes it. The add method of the repository interface is overridden to persist a book entity instance. The update method is overridden to find the given entity by its ID. If the entity instance that's found in the context or retrieved from the database isn't equal to what was passed into the update method, set the instance with the updated values to the entity in the context. Then once the transaction is committed, the changes will…

Contents