Spring Persistence Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(35 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=
* [[Spring_Framework#Spring_Framework_Core_Technologies_Concepts|Spring Framework]]
* [[Spring_Framework#Spring_Framework_Core_Technologies_Concepts|Spring Framework]]
* [[JdbcTemplate]]
* [[JdbcTemplate#Spring_Persistence_Concepts|JdbcTemplate]]
* [[Spring_Data_JPA|Spring Data JPA]]
* [[Spring_Data_JPA#Spring_Persistence_Concepts|Spring Data JPA]]
 
=Overview=


=DAO=
=DAO=
Line 8: Line 10:
=Repository=
=Repository=


[[@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 <span id='Interface_in_Application_Domain'></span>'''interface in the application domain''': 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:
 
<span id='IngredientRepository'></span><syntaxhighlight lang='java'>
public interface IngredientRepository {
 
    Ingredient findOne(String id);
 
    Iterable<Ingredient> findAll();
 
    Ingredient save(Ingredient ingredient);
}
</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]]. The Spring repositories are inspired by the repository as described in the book [[DDD|Domain-Driven Design]] by Eric Evans. From that perspective, there's one repository per [[#Aggregate_Root|Aggregate Root]].
 
Examples of repository implementations:
{{Internal|JdbcTemplate#JdbcTemplate-Based_Repository|JdbcTemplate-based Repository}}
{{Internal|Spring_Data_JPA#Spring_Data_JPA_Repository|Spring Data JPA Repository}}
 
=Aggregate Root=


The aggregate root is another concept from the book [[DDD|Domain-Driven Design]] by Eric Evans, and describes an entity which controls the lifecycle of other entities, which together are an Aggregate. An Aggregate is a subset of the data model, which is consistent between method calls to the Aggregate Root.


=Object IDs=
=Object IDs=


=Concepts=
* 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#Object_IDs|Relational Databases]].
 
=Database Initialization=
{{External|[https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html Spring Database Initialization]}}


==Object IDs==
An SQL database can be initialized in several ways:
# [[Spring_Data_JPA#Generic_JPA_Database_Initialization|Database Initialization with JPA]]
# [[Spring_Data_JPA#Database_Initialization_Using_Hibernate|Database Initialization with Hibernate]]
# [[Spring_Boot_Concepts#Spring_Boot_Database_Initialization|Spring Boot Database Initialization]]
# Database Initialization with a High-Level Database Migration Tool such as [[Spring_Flyway_Support#Database_Migration_on_Startup|Flyway]] or Liquibase.


* 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#Object_IDs|Relational Databases]].
 
<font color=darkgray>
Clarify and classify these:
 
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
 
spring.jpa.hibernate.ddl-auto=create
 
hibernate.hbm2ddl.auto=create
</font>
 
=DataSource=
 
If support for JDBC persistence is present in an application, we can access a DataSource as follows:
 
<syntaxhighlight lang='java'>
import javax.sql.DataSource;
public class SomeClass {
 
    private DataSource dataSource;
 
    public SomeClass(@Autowired DataSource dataSource) {
        this.dataSource = dataSource;
    }
 
    ...
}
 
</syntaxhighlight>

Latest revision as of 22:19, 3 December 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 in the application domain: 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. From that perspective, there's one repository per Aggregate Root.

Examples of repository implementations:

JdbcTemplate-based Repository
Spring Data JPA Repository

Aggregate Root

The aggregate root is another concept from the book Domain-Driven Design by Eric Evans, and describes an entity which controls the lifecycle of other entities, which together are an Aggregate. An Aggregate is a subset of the data model, which is consistent between method calls to the Aggregate Root.

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.

Database Initialization

Spring Database Initialization

An SQL database can be initialized in several ways:

  1. Database Initialization with JPA
  2. Database Initialization with Hibernate
  3. Spring Boot Database Initialization
  4. Database Initialization with a High-Level Database Migration Tool such as Flyway or Liquibase.


Clarify and classify these:

spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.hibernate.ddl-auto=create
hibernate.hbm2ddl.auto=create

DataSource

If support for JDBC persistence is present in an application, we can access a DataSource as follows:

import javax.sql.DataSource;
public class SomeClass {

    private DataSource dataSource;

    public SomeClass(@Autowired DataSource dataSource) {
        this.dataSource = dataSource;
    }

    ...
}