Cascade types don’t seem to work with the entity manager that axon uses. If I use CascadeType.Persist, the second time I try to create an object with the same child I get a duplicate key constraint error. If I use CascadeType.Merge, it won’t work if the entries do not exist in the database.
Tried using different CascadeTypes, also the hibernate @Cascade annotations but with no avail. Also tried to use @AggregateMember for the children but the children entities don’t seem to be saved before the parent.
@Aggregate
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = {"id"}, callSuper = false)
@Builder
public class OfficialRecordEntity {
@Id
@AggregateIdentifier
private String id;
//many other fields
@OneToMany(cascade = CascadeType.PERSIST)
private List<Accessory> accessories;
@ManyToOne(cascade = CascadeType.PERSIST)
private ExpeditionType expeditionType;
//many other commandhandlers/eventhandlers
@CommandHandler
public OfficialRecordEntity(CreateOfficialRecordCommand command) {
apply(new CreateOfficialRecordEvent(command.getId(), command.getOfficialRecordDTO()));
}
@EventSourcingHandler
public void on(final CreateOfficialRecordEvent event) {
//...some field initializations
}
}
`
`
Asked this question a few days ago on stackoverflow here. Steven was kind enough to give me some tips but I didn’t manage to make much progress. I greated a github repo with what I’ve been trying out with his tips. The issue I have been having now with this solution is “Command ‘com.example.axon.domain.commands.CreateCourseCommand’ resulted in javax.persistence.PersistenceException(org.hibernate.PersistentObjectException: detached entity passed to persist: com.example.axon.model.Student)”. It seems that if the object already exists, it doesn’t attempt to override it, it just complains that it is detached…
So if anyone would be kind enough to give it a look and lead me on the right path I would really appreciate it!