Spring Persistence Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 23: Line 23:
</syntaxhighlight>
</syntaxhighlight>


Any repository interface defined as shown needs an implementation that performs the access to a specific persistence resource, depending on the persistence technology used. For example, a JDBC-based repository needs access to a [[DataSource#Overview|JDBC DataSource]] or a higher-level [[JdbcTemplate]], while a JPA-based repository needs access to an [[JPA Concepts#EntityManager|EntityManager]]. The repository implementation class can be configured with XML, or can be annotated with the [[@Repository]] [[Spring_Dependency_Injection_and_Inversion_of_Control_Container_Concepts#Stereotype|stereotype annotation]].
Any repository interface defined as shown needs an implementation that performs the access to a specific persistence resource, depending on the persistence technology used. For example, a JDBC-based repository needs access to a [[DataSource#Overview|JDBC DataSource]] or a higher-level [[JdbcTemplate]], while a JPA-based repository needs access to an [[JPA Concepts#EntityManager|EntityManager]]. The repository implementation class can be configured with XML, or can be annotated with the [[@Repository]] [[Spring_Dependency_Injection_and_Inversion_of_Control_Container_Concepts#Stereotype|stereotype annotation]]. The Spring repositories are inspired by the repository as described in the book [[DDD|Domain-Driven Design]] by Eric Evans.


Examples of repository implementations:
Examples of repository implementations:

Revision as of 16:49, 15 October 2018

Internal

Overview

DAO

Repository

A repository is a Data Access Layer (or Persistence Layer) application component that intermediates interaction with a database: all database-related operations are done though the repository. The repository object conceals all low-level operations that are required by the interaction with the database and exposes to the application only high level operations such as queries, save, etc., all these taking domain model object instance arguments. It is a good practice to define a repository as an interface that exposes domain model-typed API, without any binding to a specific persistence technology. The following example defines a repository of "ingredients", where an "ingredient" is a domain model specific concept:

public interface IngredientRepository {

    Ingredient findOne(String id);

    Iterable<Ingredient> findAll();

    Ingredient save(Ingredient ingredient);
}

Any repository interface defined as shown needs an implementation that performs the access to a specific persistence resource, depending on the persistence technology used. For example, a JDBC-based repository needs access to a JDBC DataSource or a higher-level JdbcTemplate, while a JPA-based repository needs access to an EntityManager. The repository implementation class can be configured with XML, or can be annotated with the @Repository stereotype annotation. The Spring repositories are inspired by the repository as described in the book Domain-Driven Design by Eric Evans.

Examples of repository implementations:

JdbcTemplate-based Repository
Spring Data JPA Repository

Object IDs

  • When persisting objects in a relational database, it is generally a good idea to have one field in the object that uniquely identifies the object. See Relational Databases.