You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
664 lines
29 KiB
664 lines
29 KiB
[[jdbc.repositories]] |
|
= JDBC Repositories |
|
|
|
This chapter points out the specialties for repository support for JDBC. This builds on the core repository support explained in <<repositories>>. |
|
You should have a sound understanding of the basic concepts explained there. |
|
|
|
[[jdbc.why]] |
|
=== Why Spring Data JDBC? |
|
|
|
The main persistence API for relational databases in the Java world is certainly JPA, which has its own Spring Data module. |
|
Why is there another one? |
|
|
|
JPA does a lot of things in order to help the developer. |
|
Among other things, it tracks changes to entities. |
|
It does lazy loading for you. |
|
It lets you map a wide array of object constructs to an equally wide array of database designs. |
|
|
|
This is great and makes a lot of things really easy. |
|
Just take a look at a basic JPA tutorial. |
|
But it often gets really confusing as to why JPA does a certain thing. |
|
Also, things that are really simple conceptually get rather difficult with JPA. |
|
|
|
Spring Data JDBC aims to be much simpler conceptually, by embracing the following design decisions: |
|
|
|
* If you load an entity, SQL statements get executed. |
|
Once this is done, you have a completely loaded entity. |
|
No lazy loading or caching is done. |
|
|
|
* If you save an entity, it gets saved. |
|
If you do not, it does not. |
|
There is no dirty tracking and no session. |
|
|
|
* There is a simple model of how to map entities to tables. |
|
It probably only works for rather simple cases. |
|
If you do not like that, you should code your own strategy. |
|
Spring Data JDBC offers only very limited support for customizing the strategy with annotations. |
|
|
|
[[jdbc.domain-driven-design]] |
|
=== Domain Driven Design and Relational Databases. |
|
|
|
All Spring Data modules are inspired by the concepts of "`repository`", "`aggregate`", and "`aggregate root`" from Domain Driven Design. |
|
These are possibly even more important for Spring Data JDBC, because they are, to some extent, contrary to normal practice when working with relational databases. |
|
|
|
An aggregate is a group of entities that is guaranteed to be consistent between atomic changes to it. |
|
A classic example is an `Order` with `OrderItems`. |
|
A property on `Order` (for example, `numberOfItems` is consistent with the actual number of `OrderItems`) remains consistent as changes are made. |
|
|
|
References across aggregates are not guaranteed to be consistent at all times. |
|
They are guaranteed to become consistent eventually. |
|
|
|
Each aggregate has exactly one aggregate root, which is one of the entities of the aggregate. |
|
The aggregate gets manipulated only through methods on that aggregate root. |
|
These are the atomic changes mentioned earlier. |
|
|
|
A repository is an abstraction over a persistent store that looks like a collection of all the aggregates of a certain type. |
|
For Spring Data in general, this means you want to have one `Repository` per aggregate root. |
|
In addition, for Spring Data JDBC this means that all entities reachable from an aggregate root are considered to be part of that aggregate root. |
|
Spring Data JDBC assumes that only the aggregate has a foreign key to a table storing non-root entities of the aggregate and no other entity points toward non-root entities. |
|
|
|
WARNING: In the current implementation, entities referenced from an aggregate root are deleted and recreated by Spring Data JDBC. |
|
|
|
You can overwrite the repository methods with implementations that match your style of working and designing your database. |
|
|
|
[[jdbc.java-config]] |
|
=== Annotation-based Configuration |
|
The Spring Data JDBC repositories support can be activated by an annotation through Java configuration, as the following example shows: |
|
|
|
.Spring Data JDBC repositories using Java configuration |
|
==== |
|
[source, java] |
|
---- |
|
@Configuration |
|
@EnableJdbcRepositories |
|
class ApplicationConfig { |
|
|
|
@Bean |
|
public DataSource dataSource() { |
|
|
|
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); |
|
return builder.setType(EmbeddedDatabaseType.HSQL).build(); |
|
} |
|
|
|
} |
|
---- |
|
==== |
|
|
|
The configuration class in the preceding example sets up an embedded HSQL database by using the `EmbeddedDatabaseBuilder` API of `spring-jdbc`. |
|
We activate Spring Data JDBC repositories by using the `@EnableJdbcRepositories`. |
|
If no base package is configured, it uses the package in which the configuration class resides. |
|
|
|
[[jdbc.entity-persistence]] |
|
== Persisting Entities |
|
|
|
Saving an aggregate can be performed with the `CrudRepository.save(…)` method. |
|
If the aggregate is new, this results in an insert for the aggregate root, followed by insert statements for all directly or indirectly referenced entities. |
|
|
|
If the aggregate root is not new, all referenced entities get deleted, the aggregate root gets updated, and all referenced entities get inserted again. |
|
Note that whether an instance is new is part of the instance's state. |
|
|
|
NOTE: This approach has some obvious downsides. |
|
If only few of the referenced entities have been actually changed, the deletion and insertion is wasteful. |
|
While this process could and probably will be improved, there are certain limitations to what Spring Data JDBC can offer. |
|
It does not know the previous state of an aggregate. |
|
So any update process always has to take whatever it finds in the database and make sure it converts it to whatever is the state of the entity passed to the save method. |
|
|
|
include::{spring-data-commons-docs}/object-mapping.adoc[leveloffset=+2] |
|
|
|
[[jdbc.entity-persistence.types]] |
|
=== Supported Types in Your Entity |
|
|
|
The properties of the following types are currently supported: |
|
|
|
* All primitive types and their boxed types (`int`, `float`, `Integer`, `Float`, and so on) |
|
|
|
* Enums get mapped to their name. |
|
|
|
* `String` |
|
|
|
* `java.util.Date`, `java.time.LocalDate`, `java.time.LocalDateTime`, and `java.time.LocalTime` |
|
|
|
* Arrays and Collections of the types mentioned above can be mapped to columns of array type if your database supports that. |
|
|
|
* Anything your database driver accepts. |
|
|
|
* References to other entities. They are considered a one-to-one relationship, or an embedded type. |
|
It is optional for one-to-one relationship entities to have an `id` attribute. |
|
The table of the referenced entity is expected to have an additional column named the same as the table of the referencing entity. |
|
You can change this name by implementing `NamingStrategy.getReverseColumnName(RelationalPersistentProperty property)`. |
|
Embedded entities do not need an `id`. |
|
If one is present it gets ignored. |
|
|
|
* `Set<some entity>` is considered a one-to-many relationship. |
|
The table of the referenced entity is expected to have an additional column named the same as the table of the referencing entity. |
|
You can change this name by implementing `NamingStrategy.getReverseColumnName(RelationalPersistentProperty property)`. |
|
|
|
* `Map<simple type, some entity>` is considered a qualified one-to-many relationship. |
|
The table of the referenced entity is expected to have two additional columns: One named the same as the table of the referencing entity for the foreign key and one with the same name and an additional `_key` suffix for the map key. |
|
You can change this behavior by implementing `NamingStrategy.getReverseColumnName(RelationalPersistentProperty property)` and `NamingStrategy.getKeyColumn(RelationalPersistentProperty property)`, respectively. |
|
Alternatively you may annotate the attribute with `@MappedCollection(idColumn="your_column_name", keyColumn="your_key_column_name")` |
|
|
|
* `List<some entity>` is mapped as a `Map<Integer, some entity>`. |
|
|
|
The handling of referenced entities is limited. |
|
This is based on the idea of aggregate roots as described above. |
|
If you reference another entity, that entity is, by definition, part of your aggregate. |
|
So, if you remove the reference, the previously referenced entity gets deleted. |
|
This also means references are 1-1 or 1-n, but not n-1 or n-m. |
|
|
|
If you have n-1 or n-m references, you are, by definition, dealing with two separate aggregates. |
|
References between those should be encoded as simple `id` values, which should map properly with Spring Data JDBC. |
|
|
|
|
|
[[jdbc.entity-persistence.custom-converters]] |
|
=== Custom converters |
|
|
|
Custom converters can be registered, for types that are not supported by default, by inheriting your configuration from `JdbcConfiguration` and overwriting the method `jdbcCustomConversions()`. |
|
|
|
==== |
|
[source, java] |
|
---- |
|
@Configuration |
|
public class DataJdbcConfiguration extends JdbcConfiguration { |
|
|
|
@Override |
|
protected JdbcCustomConversions jdbcCustomConversions() { |
|
|
|
return new JdbcCustomConversions(Collections.singletonList(TimestampTzToDateConverter.INSTANCE)); |
|
|
|
} |
|
|
|
@ReadingConverter |
|
enum TimestampTzToDateConverter implements Converter<TIMESTAMPTZ, Date> { |
|
|
|
INSTANCE; |
|
|
|
@Override |
|
public Date convert(TIMESTAMPTZ source) { |
|
//... |
|
} |
|
} |
|
} |
|
---- |
|
==== |
|
|
|
The constructor of `JdbcCustomConversions` accepts a list of `org.springframework.core.convert.converter.Converter`. |
|
|
|
Converters should be annotated with `@ReadingConverter` or `@WritingConverter` in order to control their applicability to only reading from or to writing to the database. |
|
|
|
`TIMESTAMPTZ` in the example is a database specific data type that needs conversion into something more suitable for a domain model. |
|
|
|
==== JdbcValue |
|
|
|
Value conversion uses `JdbcValue` to enrich values propagated to JDBC operations with a `java.sql.Types` type. |
|
Register a custom write converter if you need to specify a JDBC-specific type instead of using type derivation. |
|
This converter should convert the value to `JdbcValue` which has a field for the value and for the actual `JDBCType`. |
|
|
|
[[jdbc.entity-persistence.naming-strategy]] |
|
=== `NamingStrategy` |
|
|
|
When you use the standard implementations of `CrudRepository` that Spring Data JDBC provides, they expect a certain table structure. |
|
You can tweak that by providing a {javadoc-base}org/springframework/data/relational/core/mapping/NamingStrategy.html[`NamingStrategy`] in your application context. |
|
|
|
[[jdbc.entity-persistence.custom-table-name]] |
|
=== `Custom table names` |
|
|
|
When the NamingStrategy does not matching on your database table names, you can customize the names with the {javadoc-base}org/springframework/data/relational/core/mapping/Table.html[`@Table`] annotation. |
|
The element `value` of this annotation provides the custom table name. The following example maps the `MyEntity` class to the `CUSTOM_TABLE_NAME` table in the database: |
|
|
|
==== |
|
[source, java] |
|
---- |
|
@Table("CUSTOM_TABLE_NAME") |
|
public class MyEntity { |
|
@Id |
|
Integer id; |
|
|
|
String name; |
|
} |
|
---- |
|
==== |
|
|
|
[[jdbc.entity-persistence.custom-column-name]] |
|
=== `Custom column names` |
|
|
|
When the NamingStrategy does not matching on your database column names, you can customize the names with the {javadoc-base}org/springframework/data/relational/core/mapping/Column.html[`@Column`] annotation. |
|
The element `value` of this annotation provides the custom column name. |
|
The following example maps the `name` property of the `MyEntity` class to the `CUSTOM_COLUMN_NAME` column in the database: |
|
|
|
==== |
|
[source, java] |
|
---- |
|
public class MyEntity { |
|
@Id |
|
Integer id; |
|
|
|
@Column("CUSTOM_COLUMN_NAME") |
|
String name; |
|
} |
|
---- |
|
==== |
|
|
|
The {javadoc-base}org/springframework/data/relational/core/mapping/MappedCollection.html[`@MappedCollection`] annotation can be used on a reference type (one-to-one relationship) or on Sets, Lists, and Maps (one-to-many relationship) |
|
On all these types the `value` element of the annotation is used to provide a custom name for the foreign key column referencing the id column in the other table. |
|
In the following example the corresponding table for the `MySubEntity` class has a name column, and the id column of the `MyEntity` id for relationship reasons. |
|
The name of this `MySubEntity` class's id column can also be customized with the `idColumn` element of the {javadoc-base}org/springframework/data/relational/core/mapping/MappedCollection.html[`@MappedCollection`] annotation: |
|
|
|
==== |
|
[source, java] |
|
---- |
|
public class MyEntity { |
|
@Id |
|
Integer id; |
|
|
|
@MappedCollection(idColumn = "CUSTOM_COLUMN_NAME") |
|
Set<MySubEntity> name; |
|
} |
|
|
|
public class MySubEntity { |
|
String name; |
|
} |
|
---- |
|
==== |
|
|
|
When using `List` and `Map` you must have an additional column for the position of a dataset in the `List` or the key value of the entity in the `Map`. |
|
This additional column name may be customized with the `keyColumn` Element of the {javadoc-base}org/springframework/data/relational/core/mapping/MappedCollection.html[`@MappedCollection`] annotation: |
|
|
|
==== |
|
[source, java] |
|
---- |
|
public class MyEntity { |
|
@Id |
|
Integer id; |
|
|
|
@MappedCollection(idColumn = "CUSTOM_COLUMN_NAME", keyColumn = "CUSTOM_KEY_COLUMN_NAME") |
|
List<MySubEntity> name; |
|
} |
|
|
|
public class MySubEntity { |
|
String name; |
|
} |
|
---- |
|
==== |
|
|
|
[[jdbc.entity-persistence.embedded-entities]] |
|
=== Embedded entities |
|
|
|
Embedded entities are used to have value objects in your java data model, even if there is only one table in your database. |
|
In the following example you see, that `MyEntity` is mapped with the `@Embedded` annotation. |
|
The consequence of this is, that in the database a table `my_entity` with the two columns `id` and `name` (from the `EmbeddedEntity` class) is expected. |
|
|
|
==== |
|
[source, java] |
|
---- |
|
public class MyEntity { |
|
@Id |
|
Integer id; |
|
|
|
@Embedded |
|
EmbeddedEntity embeddedEntity; |
|
} |
|
|
|
public class EmbeddedEntity { |
|
String name; |
|
} |
|
---- |
|
==== |
|
|
|
If you need a value object multiple times in an entity, this can be achieved with the optional `value` element of the `@Embedded` annotation. |
|
This element represents a prefix and is prepend for each column name in the embedded object. |
|
|
|
[[jdbc.entity-persistence.state-detection-strategies]] |
|
=== Entity State Detection Strategies |
|
|
|
The following table describes the strategies that Spring Data JDBC offers for detecting whether an entity is new: |
|
|
|
.Options for detection whether an entity is new in Spring Data JDBC |
|
[options = "autowidth"] |
|
|=============== |
|
|Id-Property inspection (the default)|By default, Spring Data JDBC inspects the identifier property of the given entity. |
|
If the identifier property is `null`, then the entity is assumed to be new. Otherwise, it is assumed to not be new. |
|
|Implementing `Persistable`|If an entity implements `Persistable`, Spring Data JDBC delegates the new detection to the `isNew(…)` method of the entity. |
|
See the link:$$https://docs.spring.io/spring-data/data-commons/docs/current/api/index.html?org/springframework/data/domain/Persistable.html$$[Javadoc] for details. |
|
|Implementing `EntityInformation`|You can customize the `EntityInformation` abstraction used in the `SimpleJdbcRepository` implementation by creating a subclass of `JdbcRepositoryFactory` and overriding the `getEntityInformation(…)` method. |
|
You then have to register the custom implementation of `JdbcRepositoryFactory` as a Spring bean. |
|
Note that this should rarely be necessary. See the link:{javadoc-base}org/springframework/data/jdbc/repository/support/JdbcRepositoryFactory.html[Javadoc] for details. |
|
|=============== |
|
|
|
[[jdbc.entity-persistence.id-generation]] |
|
=== ID Generation |
|
|
|
Spring Data JDBC uses the ID to identify entities. |
|
The ID of an entity must be annotated with Spring Data's https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/annotation/Id.html[`@Id`] annotation. |
|
|
|
When your data base has an auto-increment column for the ID column, the generated value gets set in the entity after inserting it into the database. |
|
|
|
One important constraint is that, after saving an entity, the entity must not be new any more. |
|
Note that whether an entity is new is part of the entity's state. |
|
With auto-increment columns, this happens automatically, because the ID gets set by Spring Data with the value from the ID column. |
|
If you are not using auto-increment columns, you can use a `BeforeSave` listener, which sets the ID of the entity (covered later in this document). |
|
|
|
[[jdbc.query-methods]] |
|
== Query Methods |
|
|
|
This section offers some specific information about the implementation and use of Spring Data JDBC. |
|
|
|
[[jdbc.query-methods.strategies]] |
|
=== Query Lookup Strategies |
|
|
|
The JDBC module supports defining a query manually only as a String in a `@Query` annotation. |
|
Deriving a query from the name of the method is currently not supported. |
|
|
|
[[jdbc.query-methods.at-query]] |
|
=== Using `@Query` |
|
|
|
The following example shows how to use `@Query` to declare a query method: |
|
|
|
.Declare a query method by using @Query |
|
==== |
|
[source, java] |
|
---- |
|
public interface UserRepository extends CrudRepository<User, Long> { |
|
|
|
@Query("select firstName, lastName from User u where u.emailAddress = :email") |
|
User findByEmailAddress(@Param("email") String email); |
|
} |
|
---- |
|
==== |
|
|
|
NOTE: Spring fully supports Java 8’s parameter name discovery based on the `-parameters` compiler flag. By using this flag in your build as an alternative to debug information, you can omit the `@Param` annotation for named parameters. |
|
|
|
NOTE: Spring Data JDBC supports only named parameters. |
|
|
|
|
|
[[jdbc.query-methods.at-query.custom-rowmapper]] |
|
==== Custom `RowMapper` |
|
|
|
You can configure which `RowMapper` to use, either by using the `@Query(rowMapperClass = ....)` or by registering a `RowMapperMap` bean and registering a `RowMapper` per method return type. The following example shows how to register `RowMappers`: |
|
|
|
==== |
|
[source,java] |
|
---- |
|
@Bean |
|
RowMapperMap rowMappers() { |
|
return new ConfigurableRowMapperMap() // |
|
.register(Person.class, new PersonRowMapper()) // |
|
.register(Address.class, new AddressRowMapper()); |
|
} |
|
---- |
|
==== |
|
|
|
When determining which `RowMapper` to use for a method, the following steps are followed, based on the return type of the method: |
|
|
|
. If the type is a simple type, no `RowMapper` is used. |
|
+ |
|
Instead, the query is expected to return a single row with a single column, and a conversion to the return type is applied to that value. |
|
. The entity classes in the `RowMapperMap` are iterated until one is found that is a superclass or interface of the return type in question. |
|
The `RowMapper` registered for that class is used. |
|
+ |
|
Iterating happens in the order of registration, so make sure to register more general types after specific ones. |
|
|
|
If applicable, wrapper types such as collections or `Optional` are unwrapped. |
|
Thus, a return type of `Optional<Person>` uses the `Person` type in the preceding process. |
|
|
|
|
|
[[jdbc.query-methods.at-query.modifying]] |
|
==== Modifying Query |
|
|
|
You can mark a query as being a modifying query by using the `@Modifying` on query method, as the following example shows: |
|
|
|
==== |
|
[source,java] |
|
---- |
|
@Modifying |
|
@Query("UPDATE DUMMYENTITY SET name = :name WHERE id = :id") |
|
boolean updateName(@Param("id") Long id, @Param("name") String name); |
|
---- |
|
==== |
|
|
|
You can specify the following return types: |
|
|
|
* `void` |
|
* `int` (updated record count) |
|
* `boolean`(whether a record was updated) |
|
|
|
|
|
[[jdbc.mybatis]] |
|
== MyBatis Integration |
|
|
|
For each operation in `CrudRepository`, Spring Data JDBC runs multiple statements. |
|
If there is a https://github.com/mybatis/mybatis-3/blob/master/src/main/java/org/apache/ibatis/session/SqlSessionFactory.java[`SqlSessionFactory`] in the application context, Spring Data checks, for each step, whether the `SessionFactory` offers a statement. |
|
If one is found, that statement (including its configured mapping to an entity) is used. |
|
|
|
The name of the statement is constructed by concatenating the fully qualified name of the entity type with `Mapper.` and a `String` determining the kind of statement. |
|
For example, if an instance of `org.example.User` is to be inserted, Spring Data JDBC looks for a statement named `org.example.UserMapper.insert`. |
|
|
|
When the statement is run, an instance of [`MyBatisContext`] gets passed as an argument, which makes various arguments available to the statement. |
|
|
|
The following table describes the available MyBatis statements: |
|
|
|
[cols="default,default,default,asciidoc"] |
|
|=== |
|
| Name | Purpose | CrudRepository methods that might trigger this statement | Attributes available in the `MyBatisContext` |
|
|
|
| `insert` | Inserts a single entity. This also applies for entities referenced by the aggregate root. | `save`, `saveAll`. | |
|
`getInstance`: the instance to be saved |
|
|
|
`getDomainType`: The type of the entity to be saved. |
|
|
|
`get(<key>)`: ID of the referencing entity, where `<key>` is the name of the back reference column provided by the `NamingStrategy`. |
|
|
|
|
|
| `update` | Updates a single entity. This also applies for entities referenced by the aggregate root. | `save`, `saveAll`.| |
|
`getInstance`: The instance to be saved |
|
|
|
`getDomainType`: The type of the entity to be saved. |
|
|
|
| `delete` | Deletes a single entity. | `delete`, `deleteById`.| |
|
`getId`: The ID of the instance to be deleted |
|
|
|
`getDomainType`: The type of the entity to be deleted. |
|
|
|
| `deleteAll-<propertyPath>` | Deletes all entities referenced by any aggregate root of the type used as prefix with the given property path. |
|
Note that the type used for prefixing the statement name is the name of the aggregate root, not the one of the entity to be deleted. | `deleteAll`.| |
|
|
|
`getDomainType`: The types of the entities to be deleted. |
|
|
|
| `deleteAll` | Deletes all aggregate roots of the type used as the prefix | `deleteAll`.| |
|
|
|
`getDomainType`: The type of the entities to be deleted. |
|
|
|
| `delete-<propertyPath>` | Deletes all entities referenced by an aggregate root with the given propertyPath | `deleteById`.| |
|
|
|
`getId`: The ID of the aggregate root for which referenced entities are to be deleted. |
|
|
|
`getDomainType`: The type of the entities to be deleted. |
|
|
|
| `findById` | Selects an aggregate root by ID | `findById`.| |
|
|
|
`getId`: The ID of the entity to load. |
|
|
|
`getDomainType`: The type of the entity to load. |
|
|
|
| `findAll` | Select all aggregate roots | `findAll`.| |
|
|
|
`getDomainType`: The type of the entity to load. |
|
|
|
| `findAllById` | Select a set of aggregate roots by ID values | `findAllById`.| |
|
|
|
`getId`: A list of ID values of the entities to load. |
|
|
|
`getDomainType`: The type of the entity to load. |
|
|
|
| `findAllByProperty-<propertyName>` | Select a set of entities that is referenced by another entity. The type of the referencing entity is used for the prefix. The referenced entities type is used as the suffix. | All `find*` methods.| |
|
|
|
`getId`: The ID of the entity referencing the entities to be loaded. |
|
|
|
`getDomainType`: The type of the entity to load. |
|
|
|
| `count` | Count the number of aggregate root of the type used as prefix | `count` | |
|
|
|
`getDomainType`: The type of aggregate roots to count. |
|
|=== |
|
|
|
[[jdbc.events]] |
|
== Events |
|
|
|
Spring Data JDBC triggers events that get published to any matching `ApplicationListener` in the application context. |
|
For example, the following listener gets invoked before an aggregate gets saved: |
|
|
|
==== |
|
[source,java] |
|
---- |
|
@Bean |
|
public ApplicationListener<BeforeSave> timeStampingSaveTime() { |
|
|
|
return event -> { |
|
|
|
Object entity = event.getEntity(); |
|
if (entity instanceof Category) { |
|
Category category = (Category) entity; |
|
category.timeStamp(); |
|
} |
|
}; |
|
} |
|
---- |
|
==== |
|
|
|
The following table describes the available events: |
|
|
|
.Available events |
|
|=== |
|
| Event | When It Is Published |
|
|
|
| {javadoc-base}org/springframework/data/relational/core/mapping/event/BeforeDeleteEvent.html[`BeforeDeleteEvent`] |
|
| Before an aggregate root gets deleted. |
|
|
|
| {javadoc-base}org/springframework/data/relational/core/mapping/event/AfterDeleteEvent.html[`AfterDeleteEvent`] |
|
| After an aggregate root gets deleted. |
|
|
|
| {javadoc-base}/org/springframework/data/relational/core/mapping/event/BeforeSaveEvent.html[`BeforeSaveEvent`] |
|
| Before an aggregate root gets saved (that is, inserted or updated but after the decision about whether if it gets updated or deleted was made). |
|
The event has a reference to an {javadoc-base}/org/springframework/data/relational/core/conversion/AggregateChange.html[`AggregateChange`] instance. |
|
The instance can be modified by adding or removing {javadoc-base}/org/springframework/data/relational/core/conversion/DbAction.html[`DbAction`] instances. |
|
|
|
| {javadoc-base}org/springframework/data/relational/core/mapping/event/AfterSaveEvent.html[`AfterSaveEvent`] |
|
| After an aggregate root gets saved (that is, inserted or updated). |
|
|
|
| {javadoc-base}org/springframework/data/relational/core/mapping/event/AfterLoadEvent.html[`AfterLoadEvent`] |
|
| After an aggregate root gets created from a database `ResultSet` and all its property get set. |
|
|=== |
|
|
|
[[jdbc.logging]] |
|
== Logging |
|
|
|
Spring Data JDBC does little to no logging on its own. |
|
Instead, the mechanics of `JdbcTemplate` to issue SQL statements provide logging. |
|
Thus, if you want to inspect what SQL statements are executed, activate logging for Spring's {spring-framework-docs}/data-access.html#jdbc-JdbcTemplate[`NamedParameterJdbcTemplate`] or http://www.mybatis.org/mybatis-3/logging.html[MyBatis]. |
|
|
|
[[jdbc.transactions]] |
|
== Transactionality |
|
CRUD methods on repository instances are transactional by default. |
|
For reading operations, the transaction configuration `readOnly` flag is set to `true`. All others are configured with a plain `@Transactional` annotation so that default transaction configuration applies. |
|
For details, see the Javadoc of link:{javadoc-base}org/springframework/data/jdbc/repository/support/SimpleJdbcRepository.html[`SimpleJdbcRepository`]. If you need to tweak transaction configuration for one of the methods declared in a repository, redeclare the method in your repository interface, as follows: |
|
|
|
.Custom transaction configuration for CRUD |
|
==== |
|
[source, java] |
|
---- |
|
public interface UserRepository extends CrudRepository<User, Long> { |
|
|
|
@Override |
|
@Transactional(timeout = 10) |
|
public List<User> findAll(); |
|
|
|
// Further query method declarations |
|
} |
|
---- |
|
==== |
|
|
|
The preceding causes the `findAll()` method to be executed with a timeout of 10 seconds and without the `readOnly` flag. |
|
|
|
Another way to alter transactional behavior is by using a facade or service implementation that typically covers more than one repository. Its purpose is to define transactional boundaries for non-CRUD operations. The following example shows how to create such a facade: |
|
|
|
.Using a facade to define transactions for multiple repository calls |
|
==== |
|
[source, java] |
|
---- |
|
@Service |
|
class UserManagementImpl implements UserManagement { |
|
|
|
private final UserRepository userRepository; |
|
private final RoleRepository roleRepository; |
|
|
|
@Autowired |
|
public UserManagementImpl(UserRepository userRepository, |
|
RoleRepository roleRepository) { |
|
this.userRepository = userRepository; |
|
this.roleRepository = roleRepository; |
|
} |
|
|
|
@Transactional |
|
public void addRoleToAllUsers(String roleName) { |
|
|
|
Role role = roleRepository.findByName(roleName); |
|
|
|
for (User user : userRepository.findAll()) { |
|
user.addRole(role); |
|
userRepository.save(user); |
|
} |
|
} |
|
---- |
|
==== |
|
|
|
The preceding example causes calls to `addRoleToAllUsers(…)` to run inside a transaction (participating in an existing one or creating a new one if none are already running). The transaction configuration for the repositories is neglected, as the outer transaction configuration determines the actual repository to be used. Note that you have to explicitly activate `<tx:annotation-driven />` or use `@EnableTransactionManagement` to get annotation-based configuration for facades working. Note that the preceding example assumes you use component scanning. |
|
|
|
[[jdbc.transaction.query-methods]] |
|
=== Transactional Query Methods |
|
To let your query methods be transactional, use `@Transactional` at the repository interface you define, as the following example shows: |
|
|
|
.Using @Transactional at query methods |
|
==== |
|
[source, java] |
|
---- |
|
@Transactional(readOnly = true) |
|
public interface UserRepository extends CrudRepository<User, Long> { |
|
|
|
List<User> findByLastname(String lastname); |
|
|
|
@Modifying |
|
@Transactional |
|
@Query("delete from User u where u.active = false") |
|
void deleteInactiveUsers(); |
|
} |
|
---- |
|
==== |
|
|
|
Typically, you want the `readOnly` flag to be set to true, because most of the query methods only read data. In contrast to that, `deleteInactiveUsers()` uses the `@Modifying` annotation and overrides the transaction configuration. Thus, the method is with the `readOnly` flag set to `false`. |
|
|
|
NOTE: It is definitely reasonable to use transactions for read-only queries, and we can mark them as such by setting the `readOnly` flag. This does not, however, act as a check that you do not trigger a manipulating query (although some databases reject `INSERT` and `UPDATE` statements inside a read-only transaction). Instead, the `readOnly` flag is propagated as a hint to the underlying JDBC driver for performance optimizations. |
|
|
|
include::{spring-data-commons-docs}/auditing.adoc[leveloffset=+1] |
|
|
|
[[jdbc.auditing]] |
|
== JDBC Auditing |
|
|
|
In order to activate auditing, add `@EnableJdbcAuditing` to your configuration, as the following example shows: |
|
|
|
.Activating auditing with Java configuration |
|
==== |
|
[source, java] |
|
---- |
|
@Configuration |
|
@EnableJdbcAuditing |
|
class Config { |
|
|
|
@Bean |
|
public AuditorAware<AuditableUser> auditorProvider() { |
|
return new AuditorAwareImpl(); |
|
} |
|
} |
|
---- |
|
==== |
|
|
|
If you expose a bean of type `AuditorAware` to the `ApplicationContext`, the auditing infrastructure automatically picks it up and uses it to determine the current user to be set on domain types. If you have multiple implementations registered in the `ApplicationContext`, you can select the one to be used by explicitly setting the `auditorAwareRef` attribute of `@EnableJdbcAuditing`.
|
|
|