0

I am trying to evolve a domain which it includes and Aggregate-root implemented with Java Records and I am not able to find a way to use the Domain Event concept to propagate events from one Aggregate-root. https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#core.domain-events

Compilation issue with the following syntax:

import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.data.domain.AbstractAggregateRoot;

@Table("BALANCE")
public record Balance extends AbstractAggregateRoot<Balance> (

    @Id
    @Column("ID_BALANCE")
    Long balanceId,

    @Column("BALANCE")
    BigDecimal balance,

    @Column("ID_CUSTOMER")
    Long customerId,

    @Column("LAST_UPDATE")
    Timestamp lastUpdate,

    @Column("WITHDRAW_LIMIT")
    BigDecimal withdrawLimit
) {
//Business logic
}

No problem with this syntax:

import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.data.domain.AbstractAggregateRoot;

public class BalanceDemo extends AbstractAggregateRoot<BalanceDemo> {

    @Id
    @Column("ID_BALANCE")
    Long balanceId;

    @Column("BALANCE")
    BigDecimal balance;

    @Column("ID_CUSTOMER")
    Long customerId;

    @Column("LAST_UPDATE")
    Timestamp lastUpdate;

    @Column("WITHDRAW_LIMIT")
    BigDecimal withdrawLimit;

    //Constructors, Get, HashCode, Equals, toString
    //Business Logic
}

What is wrong? Is it not possible to use Java records in combination with Domain Events?

jabrena
  • 1,166
  • 3
  • 11
  • 25
  • I don't know enough about Spring Data to say whether this is possible in some way, but records cannot extend a class such as `AbstractAggregateRoot`, because they all extend `java.lang.Record`. However, in the documentation linked from the question, the example doesn't extend `AbstractAggregateRoot`, so perhaps there is some way to make it work. – Tim Moore Aug 18 '21 at 07:48

1 Answers1

1

As Tim Moore wrote in a comment a Java Record cannot extend another class since it already extends java.lang.Record implicitly.

So you can either copy the relevant code from AbstractAggregateRoot into your record or have an instance of it in your record and delegate to it in the relevant method implementations.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348