Skip to content

Commit

Permalink
Split extensions ref doc page into page with includes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Feb 13, 2025
1 parent 1f9ecd4 commit 8f3bb79
Show file tree
Hide file tree
Showing 4 changed files with 552 additions and 544 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[[core.repository-populators]]
= Repository Populators

If you work with the Spring JDBC module, you are probably familiar with the support for populating a `DataSource` with SQL scripts.
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.
Thus, the populators support XML (through Spring's OXM abstraction) and JSON (through Jackson) to define data with which to populate the repositories.

Assume you have a file called `data.json` with the following content:

.Data defined in JSON
[source,javascript]
----
[ { "_class" : "com.acme.Person",
"firstname" : "Dave",
"lastname" : "Matthews" },
{ "_class" : "com.acme.Person",
"firstname" : "Carter",
"lastname" : "Beauford" } ]
----

You can populate your repositories by using the populator elements of the repository namespace provided in Spring Data Commons.
To populate the preceding data to your `PersonRepository`, declare a populator similar to the following:

.Declaring a Jackson repository populator
[source,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/repository
https://www.springframework.org/schema/data/repository/spring-repository.xsd">
<repository:jackson2-populator locations="classpath:data.json" />
</beans>
----

The preceding declaration causes the `data.json` file to be read and deserialized by a Jackson `ObjectMapper`.

The type to which the JSON object is unmarshalled is determined by inspecting the `_class` attribute of the JSON document.
The infrastructure eventually selects the appropriate repository to handle the object that was deserialized.

To instead use XML to define the data the repositories should be populated with, you can use the `unmarshaller-populator` element.
You configure it to use one of the XML marshaller options available in Spring OXM.
See the {spring-framework-docs}/data-access/oxm.html[Spring reference documentation] for details.
The following example shows how to unmarshall a repository populator with JAXB:

.Declaring an unmarshalling repository populator (using JAXB)
[source,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/repository
https://www.springframework.org/schema/data/repository/spring-repository.xsd
http://www.springframework.org/schema/oxm
https://www.springframework.org/schema/oxm/spring-oxm.xsd">
<repository:unmarshaller-populator locations="classpath:data.json"
unmarshaller-ref="unmarshaller" />
<oxm:jaxb2-marshaller contextPath="com.acme" />
</beans>
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[[core.extensions.querydsl]]
= Querydsl Extension

http://www.querydsl.com/[Querydsl] is a framework that enables the construction of statically typed SQL-like queries through its fluent API.

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`).
Spring Data supports the fork on a best-effort basis.

Several Spring Data modules offer integration with Querydsl through `QuerydslPredicateExecutor`, as the following example shows:

.QuerydslPredicateExecutor interface
[source,java]
----
public interface QuerydslPredicateExecutor<T> {
Optional<T> findById(Predicate predicate); <1>
Iterable<T> findAll(Predicate predicate); <2>
long count(Predicate predicate); <3>
boolean exists(Predicate predicate); <4>
// … more functionality omitted.
}
----

<1> Finds and returns a single entity matching the `Predicate`.
<2> Finds and returns all entities matching the `Predicate`.
<3> Returns the number of entities matching the `Predicate`.
<4> Returns whether an entity that matches the `Predicate` exists.

To use the Querydsl support, extend `QuerydslPredicateExecutor` on your repository interface, as the following example shows:

.Querydsl integration on repositories
[source,java]
----
interface UserRepository extends CrudRepository<User, Long>, QuerydslPredicateExecutor<User> {
}
----

The preceding example lets you write type-safe queries by using Querydsl `Predicate` instances, as the following example shows:

[source,java]
----
Predicate predicate = user.firstname.equalsIgnoreCase("dave")
.and(user.lastname.startsWithIgnoreCase("mathews"));
userRepository.findAll(predicate);
----
Loading

0 comments on commit 8f3bb79

Please sign in to comment.