-1

*I am trying to migrate command handlers and EventSourcingHandler to the mapping class to tableB class. Also I am defining my command handler and event as follow.Please suggest how can I migrate the command handlers from TableA to TableB class (having the primary key embedded)

Using the concept for axon the below implementation is made. It is working in the current implementation*

     @Data
        @Aggregate(repository = "tableARepository")
        @NoArgsConstructor
        @EqualsAndHashCode(onlyExplicitlyIncluded = true)
        @Entity(name = "tableA")
        @Table(name = "tableA")
        public class TableA{
            @EmbeddedId
            @AggregateIdentifier
            @EqualsAndHashCode.Include
            private tableAId id;
        
        
            @AggregateMember(eventForwardingMode = ForwardMatchingInstances.class)
            @OneToOne(fetch = LAZY, cascade = PERSIST, mappedBy = "tableA", orphanRemoval = true)
            private TableB tableB;
        
            @CommandHandler
            public void handle(UpdateTableA command) {
                apply(new TableBUpdatedEvent(command.getAresReferenceId()));
            }
        
            @CommandHandler
            public void handle(@SuppressWarnings("unused") DeleteTableBCommand command) {
                apply(new TableADeletedEvent());
            }
        
            @EventSourcingHandler
            void on(TableAUpdatedEvent event) {
                getTableB().getTableBPK().setId(getId());
                getTableB().setReferenceId(event.getAresReference());
            }
        
            @EventSourcingHandler
            void on(TableBDeletedEvent event) {
                getTableB().setReferenceId(null);
            }
        
        }
        @Data
        @Entity(name = "TableB ")
        @Table(name = "TableB ")
        @EqualsAndHashCode(onlyExplicitlyIncluded = true)
        public class TableB {
        
            @EntityId
            @EqualsAndHashCode.Include
            @EmbeddedId
            @AttributeOverrides({ @AttributeOverride(name = "id", column = @Column(name = "ID")),
                    @AttributeOverride(name = "system", column = @Column(name = "SUB_SYSTEM")) })
            TableBPK tableBPK;
        
            @Column(name = "ID")
            private String referenceId;
        
            @Column(name = "CREATION_DATE")
            @CreationTimestamp
            private LocalDateTime creationDate;
        
            @OneToOne(optional = false)
            @JoinColumn(name = "table_ID", insertable = false, updatable = false)
            private Dossier TableA ;
        
            // This is used by lombok to use the getClass instead of instanceOf for equality
            public boolean canEqual(Object other) {
                return other != null && getClass() == other.getClass();
            }
        
        }
    
    @Data
    @Embeddable
    @NoArgsConstructor
    @RequiredArgsConstructor(staticName = "of")
    public class TableBPK implements Serializable {
    
        @NonNull
        @Embedded
        @EqualsAndHashCode.Include
        private tableAId Id;
    
        @Column(name = "SUB_SYSTEM")
        @Getter(AccessLevel.PRIVATE)
        @Setter(AccessLevel.PRIVATE)
        private Integer system = 11;
    
    }
      
   class TableBDeletedEvent
   data class TableBUpdatedEvent(val aresReference: String)
   data class UpdateTableBCommand(@field:NotNull @field:TargetAggregateIdentifier val id: tableAId ?, @field:NotNull val aresReferenceId: String)
   data class DeleteTableBCommand(@field:NotNull @field:TargetAggregateIdentifier val id: tableAId ?)
Misthi
  • 33
  • 10

1 Answers1

0

You should be able to simply do this by removing them from the TableA class and adding them to the TableB class. There is nothing in Axon really holding you back of moving that code, so it should be just that.

Granted though, when looking at your sample code, I only notice the UpdateTableA and DeleteTableBCommand commands. Without being a domain expert on your code, I can only think of the DeleteTableBCommand to be moved from TableA to TableB. But, as stated, you can simply move the code. Axon doesn't care about that.

If you are having more specific issues with your set up, I would advise to enhance your question a little further by specifying what isn't working when it comes to move these handlers around.

Steven
  • 6,936
  • 1
  • 16
  • 31