Java Magazine, May/June 2018
ORACLE COM JAVAMAGAZINE MAY JUNE 2018 30 design patterns Repository Pattern The Repository pattern is a well established pattern in enterprise applications The repository class contains all persistence related code but no business logic It provides methods to persist update and remove an entity or methods that instantiate and execute specific queries The goal of this pattern is to separate your persistence related code from your business code and to improve the reusability of your persistence related code It also makes your business code easier to read and write because you can focus on solving business requirements instead of interacting with a database If youre using Spring Data or Apache DeltaSpike youre probably already familiar with the Repository pattern Both frameworks enable you to generate repositories easily for your entities They can generate the most common create read update and delete CRUD operations and custom queries based on interfaces and method signatures The following code snippet defines a repository by extending Spring Datas CrudRepository interface and adds a method to load Employee entities with a given last name Spring Datas CrudRepository interface defines a set of methods for standard write operations such as save delete and read operations Spring Data generates a class that implements this interface So you dont need to spend any additional efort to get the required functionality public interface EmployeeRepository extends CrudRepository Employee Long List Employee findByLastname String lastname Apaches DeltaSpike project provides you with similar functionality for Java EE and Jakarta EE applications In addition to the previous structural patterns there are also several query patterns and antipatterns that you should apply or avoid when you read your data from a relational database I want to focus on the two most popular ones the Open Session in View antipattern and the DTO pattern
You must have JavaScript enabled to view digital editions.