You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/antora/modules/ROOT/pages/property-paths.adoc
+152-3Lines changed: 152 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,125 @@
1
+
[[property-paths]]
2
+
= Property Paths
3
+
4
+
This chapter covers the concept of property paths.
5
+
Property paths are a form of navigation through domain classes to apply certain aspects in the context of interacting with the model.
6
+
Application code provides property paths to data access components to express intents such as selection of properties within a query, forming predicates, or applying sorting.
7
+
A property path originates from a owning type and can consist of one to many segments.
8
+
9
+
In Spring Data, the classes that form the backbone of your persistent domain model and that are accessed through Spring Data called entities.
10
+
An entry point for the object graph is called aggregate root in alignment with domain-driven design.
11
+
12
+
[TIP]
13
+
====
14
+
Spring Data considers domain types to be entities, more specifically aggregates.
15
+
So you will see the term "entity" used throughout the documentation that can be interchanged with the term "domain type" or "aggregate".
16
+
17
+
As you might have noticed in the introduction it already hinted towards domain-driven concepts.
18
+
We consider domain objects in the sense of DDD.
19
+
Domain objects have identifiers (otherwise these would be identity-less value objects), and we somehow need to refer to identifiers when working with certain patterns to access data.
20
+
Referring to identifiers will become more meaningful as we talk about repositories and query methods.
21
+
====
22
+
23
+
[[property-path-overview]]
24
+
== Property Path Overview
25
+
26
+
Let's start from a domain model, consider the simple domain model consisting of `Person` and `Address` classes with a couple of properties each:
27
+
28
+
.Domain model
29
+
[tabs]
30
+
======
31
+
Java::
32
+
+
33
+
[source,java,role="primary"]
34
+
----
35
+
class Person {
36
+
String firstname, lastname;
37
+
int age;
38
+
Address address;
39
+
List<Address> previousAddresses;
40
+
41
+
String getFirstname() { … }; // other property accessors omitted for brevity
42
+
43
+
}
44
+
45
+
class Address {
46
+
String city, street;
47
+
48
+
// accessors omitted for brevity
49
+
50
+
}
51
+
----
52
+
53
+
Kotlin::
54
+
+
55
+
[source,kotlin,role="secondary"]
56
+
----
57
+
class Person {
58
+
var firstname: String? = null
59
+
var lastname: String? = null
60
+
var age: Int = 0
61
+
var address: Address? = null
62
+
var previousAddresses: List<Address> = emptyList()
63
+
}
64
+
65
+
class Address {
66
+
var city: String? = null
67
+
var street: String? = null
68
+
}
69
+
----
70
+
======
71
+
72
+
Property paths let you express references to properties using dot-path notation, for example when declaring sort orders:
73
+
74
+
.Sorting
75
+
[source,java]
76
+
----
77
+
Sort.by("firstname", "lastname")
78
+
----
79
+
80
+
Dot-path notation addresses individual properties that are separated by a dot `.`.
81
+
Methods accepting a `String` property path typically allow usage of single-segment property paths (i.e. reference to a top-level property) or multi-segment properties unless otherwise indicated.
82
+
83
+
Dot-path traversal into properties considers the component type of array and collection properties allowing references into the actual type for a simple navigation across collection-like types.
84
+
85
+
----
86
+
Sort.by("address.city") <1>
87
+
88
+
Sort.by("previousAddresses") <2>
89
+
90
+
Sort.by("previousAddresses.city") <3>
91
+
----
92
+
93
+
<1> Sort by the `city` of the `address` object.
94
+
<2> Sort by the `Person.previousAddresses` collection.
95
+
Applicable for technologies that support sorting by arrays or lists
96
+
<3> Sort by the `city` of any previous address.
97
+
98
+
String-based property paths are simple and widely applicable because they do not require additional dependencies.
99
+
There are various tradeoffs to consider:
100
+
101
+
* Simple and flexible: Generalized and simple usage by expressing a property reference as string literal or constant.
102
+
* Untyped: String paths do not carry compile-time type information and do not have a dependency on the underlying domain type.
103
+
* Context-less: Without the associated domain type, string literals are easy to miss when renaming or refactoring the model.
104
+
105
+
For better refactoring safety and stronger type consistency, prefer type-safe property references that associate property paths with type information, enabling IDE refactoring and compiler validation.
106
+
<<type-safe-property-references>> shows how to use method references to express property paths in a type-safe manner.
107
+
108
+
NOTE: For advanced usage: Property paths are translated to javadoc:org.springframework.data.core.PropertyPath[], read more about <<property-path-internals>>.
109
+
110
+
[[property-path-internals]]
111
+
=== Property Path Internals
112
+
113
+
The `org.springframework.data.core` package is the basis for Spring Data's navigation across domain classes.
114
+
The javadoc:org.springframework.data.core.TypeInformation[] inteface provides type introspection capable of resolving the type of a property. javadoc:org.springframework.data.core.PropertyPath[] represents a textual navigation path through a domain class.
115
+
Together they provide:
116
+
117
+
* Type introspection and resolution of generics
118
+
* Creation and validation of property paths
119
+
* Actual type resolution for properties that hold a objects of an actual type such as collections and maps
120
+
1
121
[[type-safe-property-references]]
2
-
= Type-safe Property References
122
+
== Type-safe Property References
3
123
4
124
Type-safe property references address a common source of friction in data access code: The reliance on literal, dot-separated property path strings.
5
125
Such stringly-typed references are fragile during refactoring difficult to identify as they often lack context.
@@ -9,12 +129,28 @@ A property path is a simple, transportable representation of object navigation.
9
129
When expressed as a method-reference the compiler participates in validation and IDEs provide meaningful refactoring support.
10
130
The result is code that reads naturally, fails fast on renames, and integrates cleanly with existing query and sorting abstractions, for example:
0 commit comments