


























TIP
Projection is a subset of an aggregate loaded from a repository for read-only purposes.
Methods returning projections are typically defined on the repository level, making the repository interface aware of all possible types of projections used in the application.
java
package com.app.account.domain;
public interface AccountRepository extends Repository<Account, String> {
AccountBasic findAccountBasicById(String id);
AccountComplete findAccountCompleteById(String id);
}
public record AccountBasic(String id,
String iban,
String bic) {}
public record AccountComplete(String id,
String iban,
String bic,
State state,
Type type,
LocalDateTime createdAt) {}This approach has some drawbacks:
AccountBasic? AccountLight? AccountData? All these names are quite meaningless.Instead of making the repository aware of all types projections used by all the use cases, we can use dynamic projections.
Dynamic Projections is one of the lesser known and used feature of Spring Data JPA.
Dynamic projections let you define a generic method on the repository level that takes any kind of projection, and remain unaware of the shape of projections.
java
package com.app.account.domain;
public interface AccountRepository extends Repository<Account, String> {
<T> T findById(String id, Class<T> projection);
}Now, in any package we can define a projection interface or a record, which as long as it matches the structure of an Account entity, can be used with this dynamic repository method:
java
package com.app.account;
record AccountBasic(String id,
String iban,
String bic) {}
// ...
AccountBasic account = accountRepository.findById(id, AccountBasic.class);In this particular example AccountBasic is a package private class located in com.app.account. Only classes from this package can access it and as a result, the risk of abusing this projection in other parts of the application does not exist.
We can take hiding the projections visibility to the next level by using local records.
Records that are records defined inside a method are called local records and become a perfect solution for use cases, where the projection is used only inside of this method and are not returned:
java
class BankTransferService {
private final AccountRepository accountRepository;
// ...
void transferMoney(String sourceAccountId, String targetAccountId) {
record AccountBasic(String id,
String iban,
String bic) {}
var source = accountRepository.findById(sourceAccountId, AccountBasic.class);
var target = accountRepository.findById(targetAccountId, AccountBasic.class);
// ...
}
}Just in case you are thinking now about using classes the same way, perhaps don't because local records and local classes are different:
Like nested record classes, local record classes are implicitly static, which means that their own methods can't access any variables of the enclosing method, unlike local classes, which are never static.
https://docs.oracle.com/en/java/javase/17/language/records.html
As usual, there are some downsides of this approach worth taking into consideration:
where part of repository method returning a dynamic projection is resolved only from the method name - it is not possible to write a custom JPQL or SQL queryHope you found it useful or at least slightly interesting 😉 Feel free to drop a comment if you found any mistake or have a question. Also, feel free to reach out to me on twitter.com/maciejwalkowiak.
Let's stay in touch and follow me on Twitter: @maciejwalkowiak
Subscribe to RSS feed ![]()
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。