a.k.a Demo
spring-boot-starter-data-jdbc
Aggregate Roots need an @Id
No getters/setter required
You can "wither" instead
We have already JPA
Complexity is tempting
a.k.a more demo code
Clone: set ID to null
and save again.
Loading the same entity creates multiple instances.
and it costs you your life.
CRUD Operations for entities with references
But what about Many-to-X?
Aggregate is a pattern in Domain-Driven Design. A DDD aggregate is a cluster of domain objects that can be treated as a single unit.
Aggregates are the basic element of transfer of data storage - you request to load or save whole aggregates.
An aggregate will have one of its component objects be the aggregate root. Any references from outside the aggregate should only go to the aggregate root. The root can thus ensure the integrity of the aggregate as a whole.
If multiple Aggregate roots reference X,
X must be another Aggregate Root.
⇒ Not part of this Aggregate.
Everything referenced.
Just reference an id.
Makes Aggregates obvious in code
Easier testing (no huge chains of dependencies)
Allows to pick a persistence strategy per Aggregate
BeforeConvertCallback
BeforeSaveCallback
AfterSaveCallback
BeforeDeleteCallback
AfterDeleteCallback
AfterLoadEvent
AggregateChange
A model of the changes to be applied to the database
contains a list of DbAction
s.
DbAction
Represents a single SQL statement to be executed against the database.
Access the repository
change the entities
@EnableJdbcAuditing
@CreatedBy
String createdBy;
@CreatedDate
Instant createdAt;
@LastModifiedBy
String updatedBy;
@LastModifiedDate
Instant updatedAt;
AuditorAware
@Bean
AuditorAware<String> auditorAware() {
return new ModifiableAuditorAware();
}
AuditorAware
examplepublic class ModifiableAuditorAware
implements AuditorAware<String> {
String value;
@Override
public Optional<String> getCurrentAuditor() {
return Optional.ofNullable(value);
}
}
@Query
@Query
@Query("select count(*) from address")
int countAddresses();
@Modifying
Not promising anything.
upserts?
Dad, Board&Roleplaying Gamer, Runner, Bouldering, Freeletics Masochist
JUG Organizer