@ -10,13 +10,14 @@ This means that it does rather little out of the box. But it offers plenty of pl
@@ -10,13 +10,14 @@ This means that it does rather little out of the box. But it offers plenty of pl
== Maven Coordinates
```xml
[source,xml]
----
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
```
----
== Features
@ -24,31 +25,37 @@ This means that it does rather little out of the box. But it offers plenty of pl
@@ -24,31 +25,37 @@ This means that it does rather little out of the box. But it offers plenty of pl
In order use Spring Data JDBC you need the following:
1. An entity with an attribute marked as _id_ using the spring datas https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/annotation/Id.html[`@Id`] annotation.
1. An entity with an attribute marked as _id_ using the spring datas https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/annotation/Id.html[`@Id`] annotation.
+
[source,java]
----
public class Person {
@Id
Integer id;
}
2. A repository
public interface PersonRepository extends CrudRepository<Person, Integer> {}
3. Add `@EnableJdbcRepositories` to your application context configuration.
4. Make sure your application context contains a bean of type `DataSource`.
----
+
1. A repository
+
[source,java]
----
public interface PersonRepository extends CrudRepository<Person, Integer> {}
----
+
1. Add `@EnableJdbcRepositories` to your application context configuration.
1. Make sure your application context contains a bean of type `DataSource`.
Now you can get an instance of the repository interface injected into your beans and use it:
```java
@Autowired
private PersonRepository repository;
[source,java]
----
@Autowired
private PersonRepository repository;
public void someMethod() {
public void someMethod() {
Person person = repository.save(new Person());
}
```
}
----
=== Id generation
@ -74,9 +81,10 @@ You can tweak that by providing a https://github.com/spring-projects/spring-data
@@ -74,9 +81,10 @@ You can tweak that by providing a https://github.com/spring-projects/spring-data
Spring Data Jdbc triggers events which will get publish to any matching `ApplicationListener` in the application context.
For example the following listener will get invoked before an aggregate gets saved.
``` java
@Bean
public ApplicationListener<BeforeSave> timeStampingSaveTime() {
[source,java]
----
@Bean
public ApplicationListener<BeforeSave> timeStampingSaveTime() {
return event -> {
@ -86,27 +94,27 @@ For example the following listener will get invoked before an aggregate gets sav
@@ -86,27 +94,27 @@ For example the following listener will get invoked before an aggregate gets sav
| before an aggregate root gets saved, i.e. inserted or updated but after the decision was made if it will get updated or deleted.
The event has a reference to an https://github.com/spring-projects/spring-data-jdbc/blob/master/src/main/java/org/springframework/data/jdbc/core/conversion/AggregateChange.java[`AggregateChange`] instance.
The instance can be modified by adding or removing https://github.com/spring-projects/spring-data-jdbc/blob/master/src/main/java/org/springframework/data/jdbc/core/conversion/DbAction.java[`DbAction`]s.