This is the key. I was struggled on this for the last couple of hours and got the same result accidentally, pity on me not seeing your answer in time :(
@AggregateIdentifier
private CompanyId companyId;
By using @EmbeddedId & @Embedable annotation of JPA, I'm able to save the instance into the repository.
But, I found by default, the
GenericJpaRepository is using String as the type of identifier, so when loading the aggregate from the repository, the below exception will be thrown.
java.lang.IllegalArgumentException: Provided id of the wrong type for class com.edi.learn.axon.aggregates.BankAccount. Expected: class com.edi.learn.axon.domain.AccountId, got class java.lang.String
Since you're trying to achieve the same thing, maybe you would encounter the same issue.
My solution is provide a getter and setter method for the identifier and annotated on the getter method instead of the field itself.
@Id
public String getCompanyId() {
return companyId.toString();
}
public void setCompanyId(String companyId) {
this.companyId= new CompanyId(companyId);
}
This would avoid implementing a custom repository, which is a complex work to do.
在 2017年3月10日星期五 UTC+8下午4:43:18,Lukáš Vasek写道: