Compilation Error in Micronaut Data Repository with Kotlin Interfaces for Hibernate Reactive Entities

31 views Asked by At

I'm encountering a compilation error when defining Kotlin interfaces for my Hibernate Reactive entities and using Micronaut Data repositories. The error occurs specifically when implementing the AddressRepository.findAllByCityStateId(Long id) method.

Here are my entity classes in Java:

@Serdeable
@Entity
@Table(name = "STATE")
public class State {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SQ_STATE")
    @SequenceGenerator(name = "SQ_STATE", sequenceName = "SQ_STATE", allocationSize = 1)
    private Long id;

    @NotNull
    private String name;

    // getter and setters
}

@Serdeable
@Entity
@Table(name = "CITY")
public class City {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SQ_CITY")
    @SequenceGenerator(name = "SQ_CITY", sequenceName = "SQ_CITY", allocationSize = 1)
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "STATE_ID", nullable = false)
    private State state;

    @NotNull
    private String name;

    // getter and setters
}


@Serdeable
@Entity
@Table(name = "ADDRESS")
public class Address {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SQ_ADDRESS")
    @SequenceGenerator(name = "SQ_ADDRESS", sequenceName = "SQ_ADDRESS", allocationSize = 1)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "CITY_ID", nullable = false)
    private City city;

    @NotNull
    private String cep;

    // getter and setters
}

And here is the corresponding repository and controller:

@Repository
public interface AddressRepository extends ReactorCrudRepository<Address, Long> {
    @Join(value = "city.state")
    Flux<Address> findAllByCityStateId(Long id);
}

@Controller("/addresses")
public class AddressController {
    protected final AddressRepository addressRepository;

    public AddressController(AddressRepository addressRepository) {
        this.addressRepository = addressRepository;
    }

    @Get("/list")
    public Flux<Address> list(@NonNull Long id) {
        return addressRepository.findAllByCityStateId(id);
    }
}

When I attempt to create Kotlin interfaces for these entities, I face a compilation error:

interface State {
    var id: Long?
    var name: String
}

interface City {
    var id: Long?
    var name: String
    var state: State
}

interface Address {
    var id: Long?
    var city: City
    var cep: String
}
@Serdeable
@Entity
@Table(name = "STATE")
public class StateEntity implements State {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SQ_STATE")
    @SequenceGenerator(name = "SQ_STATE", sequenceName = "SQ_STATE", allocationSize = 1)
    private Long id;

    @NotNull
    private String name;

    // overriding getter and setters
}

@Serdeable
@Entity
@Table(name = "CITY")
public class CityEntity implements City {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SQ_CITY")
    @SequenceGenerator(name = "SQ_CITY", sequenceName = "SQ_CITY", allocationSize = 1)
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "STATE_ID", nullable = false)
    private StateEntity state;

    @NotNull
    private String name;

    public StateEntity getState() {
        return state;
    }

    public void setState(State state) {
        this.state = (StateEntity) state;
    }
    
    // overriding other getters and setters
}

@Serdeable
@Entity
@Table(name = "ADDRESS")
public class AddressEntity implements Address {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SQ_ADDRESS")
    @SequenceGenerator(name = "SQ_ADDRESS", sequenceName = "SQ_ADDRESS", allocationSize = 1)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "CITY_ID", nullable = false)
    private CityEntity city;

    @NotNull
    private String cep;

    public CityEntity getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = (CityEntity) city;
    }
    
    // overriding other getters and setters
}

The error message is:

Unable to implement Repository method: AddressRepository.findAllByCityStateId(Long id).
Cannot query entity [AddressEntity] on non-existent property: CityStateId

Is this behavior expected, or is it a bug? If it's expected, what is the recommended approach to defining Kotlin interfaces for such entity mappings in a Micronaut Data environment with Hibernate Reactive?

0

There are 0 answers