An example with Spring Data JPA
Let’s have a look at a code example that implements AccountPersistenceAdapter from the preceding figures. This adapter will have to save and load accounts to and from the database. We already saw the Account entity in Chapter 5, Implementing a Use Case, but here is its skeleton again for reference:
Note
The Account class is not a simple data class with getters and setters but instead tries to be as immutable as possible. It only provides factory methods that create an account in a valid state, and all mutating methods do some validation, such as checking the account balance before withdrawing money, so that we cannot create an invalid domain model.
We’ll use Spring Data JPA to talk to the database, so we also need @Entity-annotated classes to represent the database state of an account:
The state of an account consists merely of an ID at this stage. Later...