Skip to content

Commit 232a3da

Browse files
committed
Split extensions ref doc page into page with includes.
Closes #3243
1 parent 191300a commit 232a3da

File tree

4 files changed

+552
-544
lines changed

4 files changed

+552
-544
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[[core.repository-populators]]
2+
= Repository Populators
3+
4+
If you work with the Spring JDBC module, you are probably familiar with the support for populating a `DataSource` with SQL scripts.
5+
A similar abstraction is available on the repositories level, although it does not use SQL as the data definition language because it must be store-independent.
6+
Thus, the populators support XML (through Spring's OXM abstraction) and JSON (through Jackson) to define data with which to populate the repositories.
7+
8+
Assume you have a file called `data.json` with the following content:
9+
10+
.Data defined in JSON
11+
[source,javascript]
12+
----
13+
[ { "_class" : "com.acme.Person",
14+
"firstname" : "Dave",
15+
"lastname" : "Matthews" },
16+
{ "_class" : "com.acme.Person",
17+
"firstname" : "Carter",
18+
"lastname" : "Beauford" } ]
19+
----
20+
21+
You can populate your repositories by using the populator elements of the repository namespace provided in Spring Data Commons.
22+
To populate the preceding data to your `PersonRepository`, declare a populator similar to the following:
23+
24+
.Declaring a Jackson repository populator
25+
[source,xml]
26+
----
27+
<?xml version="1.0" encoding="UTF-8"?>
28+
<beans xmlns="http://www.springframework.org/schema/beans"
29+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
30+
xmlns:repository="http://www.springframework.org/schema/data/repository"
31+
xsi:schemaLocation="http://www.springframework.org/schema/beans
32+
https://www.springframework.org/schema/beans/spring-beans.xsd
33+
http://www.springframework.org/schema/data/repository
34+
https://www.springframework.org/schema/data/repository/spring-repository.xsd">
35+
36+
<repository:jackson2-populator locations="classpath:data.json" />
37+
38+
</beans>
39+
----
40+
41+
The preceding declaration causes the `data.json` file to be read and deserialized by a Jackson `ObjectMapper`.
42+
43+
The type to which the JSON object is unmarshalled is determined by inspecting the `_class` attribute of the JSON document.
44+
The infrastructure eventually selects the appropriate repository to handle the object that was deserialized.
45+
46+
To instead use XML to define the data the repositories should be populated with, you can use the `unmarshaller-populator` element.
47+
You configure it to use one of the XML marshaller options available in Spring OXM.
48+
See the {spring-framework-docs}/data-access/oxm.html[Spring reference documentation] for details.
49+
The following example shows how to unmarshall a repository populator with JAXB:
50+
51+
.Declaring an unmarshalling repository populator (using JAXB)
52+
[source,xml]
53+
----
54+
<?xml version="1.0" encoding="UTF-8"?>
55+
<beans xmlns="http://www.springframework.org/schema/beans"
56+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
57+
xmlns:repository="http://www.springframework.org/schema/data/repository"
58+
xmlns:oxm="http://www.springframework.org/schema/oxm"
59+
xsi:schemaLocation="http://www.springframework.org/schema/beans
60+
https://www.springframework.org/schema/beans/spring-beans.xsd
61+
http://www.springframework.org/schema/data/repository
62+
https://www.springframework.org/schema/data/repository/spring-repository.xsd
63+
http://www.springframework.org/schema/oxm
64+
https://www.springframework.org/schema/oxm/spring-oxm.xsd">
65+
66+
<repository:unmarshaller-populator locations="classpath:data.json"
67+
unmarshaller-ref="unmarshaller" />
68+
69+
<oxm:jaxb2-marshaller contextPath="com.acme" />
70+
71+
</beans>
72+
----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[[core.extensions.querydsl]]
2+
= Querydsl Extension
3+
4+
http://www.querydsl.com/[Querydsl] is a framework that enables the construction of statically typed SQL-like queries through its fluent API.
5+
6+
NOTE: Querydsl maintenance has slowed down to a point where the community has forked the project under OpenFeign at https://github.com/OpenFeign/querydsl (groupId `io.github.openfeign.querydsl`).
7+
Spring Data supports the fork on a best-effort basis.
8+
9+
Several Spring Data modules offer integration with Querydsl through `QuerydslPredicateExecutor`, as the following example shows:
10+
11+
.QuerydslPredicateExecutor interface
12+
[source,java]
13+
----
14+
public interface QuerydslPredicateExecutor<T> {
15+
16+
Optional<T> findById(Predicate predicate); <1>
17+
18+
Iterable<T> findAll(Predicate predicate); <2>
19+
20+
long count(Predicate predicate); <3>
21+
22+
boolean exists(Predicate predicate); <4>
23+
24+
// … more functionality omitted.
25+
}
26+
----
27+
28+
<1> Finds and returns a single entity matching the `Predicate`.
29+
<2> Finds and returns all entities matching the `Predicate`.
30+
<3> Returns the number of entities matching the `Predicate`.
31+
<4> Returns whether an entity that matches the `Predicate` exists.
32+
33+
To use the Querydsl support, extend `QuerydslPredicateExecutor` on your repository interface, as the following example shows:
34+
35+
.Querydsl integration on repositories
36+
[source,java]
37+
----
38+
interface UserRepository extends CrudRepository<User, Long>, QuerydslPredicateExecutor<User> {
39+
}
40+
----
41+
42+
The preceding example lets you write type-safe queries by using Querydsl `Predicate` instances, as the following example shows:
43+
44+
[source,java]
45+
----
46+
Predicate predicate = user.firstname.equalsIgnoreCase("dave")
47+
.and(user.lastname.startsWithIgnoreCase("mathews"));
48+
49+
userRepository.findAll(predicate);
50+
----

0 commit comments

Comments
 (0)