|
| 1 | +ifndef::projection-collection[] |
| 2 | +:projection-collection: Collection |
| 3 | +endif::[] |
| 4 | + |
| 5 | +[[projections.interfaces]] |
| 6 | += Interface-based Projections |
| 7 | + |
| 8 | +The easiest way to limit the result of the queries to only the name attributes is by declaring an interface that exposes accessor methods for the properties to be read, as shown in the following example: |
| 9 | + |
| 10 | +.A projection interface to retrieve a subset of attributes |
| 11 | +[source,java] |
| 12 | +---- |
| 13 | +interface NamesOnly { |
| 14 | +
|
| 15 | + String getFirstname(); |
| 16 | + String getLastname(); |
| 17 | +} |
| 18 | +---- |
| 19 | + |
| 20 | +The important bit here is that the properties defined here exactly match properties in the aggregate root. |
| 21 | +Doing so lets a query method be added as follows: |
| 22 | + |
| 23 | +.A repository using an interface based projection with a query method |
| 24 | +[source,java,subs="+attributes"] |
| 25 | +---- |
| 26 | +interface PersonRepository extends Repository<Person, UUID> { |
| 27 | +
|
| 28 | + {projection-collection}<NamesOnly> findByLastname(String lastname); |
| 29 | +} |
| 30 | +---- |
| 31 | + |
| 32 | +The query execution engine creates proxy instances of that interface at runtime for each element returned and forwards calls to the exposed methods to the target object. |
| 33 | + |
| 34 | +NOTE: Declaring a method in your `Repository` that overrides a base method (e.g. declared in `CrudRepository`, a store-specific repository interface, or the `Simple…Repository`) results in a call to the base method regardless of the declared return type. |
| 35 | +Make sure to use a compatible return type as base methods cannot be used for projections. |
| 36 | +Some store modules support `@Query` annotations to turn an overridden base method into a query method that then can be used to return projections. |
| 37 | + |
| 38 | +[[projections.interfaces.nested]] |
| 39 | +Projections can be used recursively. |
| 40 | +If you want to include some of the `Address` information as well, create a projection interface for that and return that interface from the declaration of `getAddress()`, as shown in the following example: |
| 41 | + |
| 42 | +.A projection interface to retrieve a subset of attributes |
| 43 | +[source,java] |
| 44 | +---- |
| 45 | +interface PersonSummary { |
| 46 | +
|
| 47 | + String getFirstname(); |
| 48 | + String getLastname(); |
| 49 | + AddressSummary getAddress(); |
| 50 | +
|
| 51 | + interface AddressSummary { |
| 52 | + String getCity(); |
| 53 | + } |
| 54 | +} |
| 55 | +---- |
| 56 | + |
| 57 | +On method invocation, the `address` property of the target instance is obtained and wrapped into a projecting proxy in turn. |
| 58 | + |
| 59 | +[[projections.interfaces.closed]] |
| 60 | +== Closed Projections |
| 61 | + |
| 62 | +A projection interface whose accessor methods all match properties of the target aggregate is considered to be a closed projection. |
| 63 | +The following example (which we used earlier in this chapter, too) is a closed projection: |
| 64 | + |
| 65 | +.A closed projection |
| 66 | +[source,java] |
| 67 | +---- |
| 68 | +interface NamesOnly { |
| 69 | +
|
| 70 | + String getFirstname(); |
| 71 | + String getLastname(); |
| 72 | +} |
| 73 | +---- |
| 74 | + |
| 75 | +If you use a closed projection, Spring Data can optimize the query execution, because we know about all the attributes that are needed to back the projection proxy. |
| 76 | +For more details on that, see the module-specific part of the reference documentation. |
| 77 | + |
| 78 | +[[projections.interfaces.open]] |
| 79 | +== Open Projections |
| 80 | + |
| 81 | +Accessor methods in projection interfaces can also be used to compute new values by using the `@Value` annotation, as shown in the following example: |
| 82 | + |
| 83 | +[[projections.interfaces.open.simple]] |
| 84 | +.An Open Projection |
| 85 | +[source,java] |
| 86 | +---- |
| 87 | +interface NamesOnly { |
| 88 | +
|
| 89 | + @Value("#{target.firstname + ' ' + target.lastname}") |
| 90 | + String getFullName(); |
| 91 | + … |
| 92 | +} |
| 93 | +---- |
| 94 | + |
| 95 | +The aggregate root backing the projection is available in the `target` variable. |
| 96 | +A projection interface using `@Value` is an open projection. |
| 97 | +Spring Data cannot apply query execution optimizations in this case, because the SpEL expression could use any attribute of the aggregate root. |
| 98 | + |
| 99 | +The expressions used in `@Value` should not be too complex -- you want to avoid programming in `String` variables. |
| 100 | +For very simple expressions, one option might be to resort to default methods (introduced in Java 8), as shown in the following example: |
| 101 | + |
| 102 | +[[projections.interfaces.open.default]] |
| 103 | +.A projection interface using a default method for custom logic |
| 104 | +[source,java] |
| 105 | +---- |
| 106 | +interface NamesOnly { |
| 107 | +
|
| 108 | + String getFirstname(); |
| 109 | + String getLastname(); |
| 110 | +
|
| 111 | + default String getFullName() { |
| 112 | + return getFirstname().concat(" ").concat(getLastname()); |
| 113 | + } |
| 114 | +} |
| 115 | +---- |
| 116 | + |
| 117 | +This approach requires you to be able to implement logic purely based on the other accessor methods exposed on the projection interface. |
| 118 | +A second, more flexible, option is to implement the custom logic in a Spring bean and then invoke that from the SpEL expression, as shown in the following example: |
| 119 | + |
| 120 | +[[projections.interfaces.open.bean-reference]] |
| 121 | +.Sample Person object |
| 122 | +[source,java] |
| 123 | +---- |
| 124 | +@Component |
| 125 | +class MyBean { |
| 126 | +
|
| 127 | + String getFullName(Person person) { |
| 128 | + … |
| 129 | + } |
| 130 | +} |
| 131 | +
|
| 132 | +interface NamesOnly { |
| 133 | +
|
| 134 | + @Value("#{@myBean.getFullName(target)}") |
| 135 | + String getFullName(); |
| 136 | + … |
| 137 | +} |
| 138 | +---- |
| 139 | + |
| 140 | +Notice how the SpEL expression refers to `myBean` and invokes the `getFullName(…)` method and forwards the projection target as a method parameter. |
| 141 | +Methods backed by SpEL expression evaluation can also use method parameters, which can then be referred to from the expression. |
| 142 | +The method parameters are available through an `Object` array named `args`. |
| 143 | +The following example shows how to get a method parameter from the `args` array: |
| 144 | + |
| 145 | +.Sample Person object |
| 146 | +[source,java] |
| 147 | +---- |
| 148 | +interface NamesOnly { |
| 149 | +
|
| 150 | + @Value("#{args[0] + ' ' + target.firstname + '!'}") |
| 151 | + String getSalutation(String prefix); |
| 152 | +} |
| 153 | +---- |
| 154 | + |
| 155 | +Again, for more complex expressions, you should use a Spring bean and let the expression invoke a method, as described <<projections.interfaces.open.bean-reference,earlier>>. |
| 156 | + |
| 157 | +[[projections.interfaces.nullable-wrappers]] |
| 158 | +== Nullable Wrappers |
| 159 | + |
| 160 | +Getters in projection interfaces can make use of nullable wrappers for improved null-safety. |
| 161 | +Currently supported wrapper types are: |
| 162 | + |
| 163 | +* `java.util.Optional` |
| 164 | +* `com.google.common.base.Optional` |
| 165 | +* `scala.Option` |
| 166 | +* `io.vavr.control.Option` |
| 167 | + |
| 168 | +.A projection interface using nullable wrappers |
| 169 | +[source,java] |
| 170 | +---- |
| 171 | +interface NamesOnly { |
| 172 | +
|
| 173 | + Optional<String> getFirstname(); |
| 174 | +} |
| 175 | +---- |
| 176 | + |
| 177 | +If the underlying projection value is not `null`, then values are returned using the present-representation of the wrapper type. |
| 178 | +In case the backing value is `null`, then the getter method returns the empty representation of the used wrapper type. |
0 commit comments