Hi
Ive have applying event sourcing to one of our existing entities/table called User.class.
I am using version 2.0-SNAPSHOT as I prefer the way you can customize the aggregate id
For this User I have two identifiers
public class User extends AbstractAnnotatedAggregateRoot
{
@Id
private String id;
@Column(name = “AGGREGATE_ID”)
@AggregateIdentifier
private UUID identifier;
public User(UserCreatedEvent createdEvent)
{
apply(createdEvent);
}
@EventHandler
protected void handleCreateUser(UserCreatedEvent createdEvent)
{
// Set up defaults
// Code ommitted
}
}
I also have listener to save to the query repository
public class UsersTableUpdater
{
private UserRepository userRepository;
private Repository repository;
@EventHandler
public void handleSave(AbstractUserSavedEvent savedEvent)
{
User user = repository.load(savedEvent.getIdentifier());
userRepository.save(user); // Do a merge or persist depending on whether its new or existing
}
}
When saving an entity for the first time this is fine, however when updating an existing User. My loaded User aggregate does not have my user.id or user.version (inherited from AbstractAnnotatedAggregateRoot) populated.
Because my none of my events doesn’t set values before persisting.
This causes OptimisticLocking and duplicate entity problems for me.
So my question is. What is the best way to keep the version and my primary key id, in sync in the event store. (considering I cannot set the version variable in AbstractAnnotatedAggregateRoot)
Thanks for your help. Hope my explanation was clear.
Richard