Skip to content

Repository not able to return embedded field projection #3668

@radovanradic

Description

@radovanradic

Expected Behavior

Having such embedded class

@Embeddable
public class Address {
    private final String street;
    private final String zipCode;

    public Address(String street, String zipCode) {
        this.street = street;
        this.zipCode = zipCode;
    }

    public String getStreet() {
        return street;
    }

    public String getZipCode() {
        return zipCode;
    }
}

and entity

@MappedEntity
public class Restaurant {

    @GeneratedValue
    @Id
    private Long id;
    private final String name;

    @Relation(Relation.Kind.EMBEDDED)
    private final Address address;

    @Relation(Relation.Kind.EMBEDDED)
    @MappedProperty("hqaddress_")
    @Nullable
    private Address hqAddress;


    public Restaurant(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public Address getAddress() {
        return address;
    }

    @Nullable
    public Address getHqAddress() {
        return hqAddress;
    }

    public void setHqAddress(@Nullable Address hqAddress) {
        this.hqAddress = hqAddress;
    }
}

The repository

public interface RestaurantRepository extends CrudRepository<Restaurant, Long> {
  Address findAddressById(Long id);
}

should be able to return Address for given entity id.

Actual Behaviour

This error is thrown

Cannot convert type [class java.lang.String] with value [Main] to target type: class io.micronaut.data.tck.entities.Address. Consider defining a TypeConverter bean to handle this case.
io.micronaut.data.exceptions.DataAccessException: Cannot convert type [class java.lang.String] with value [abd] to target type: class io.micronaut.data.tck.entities.Address. Consider defining a TypeConverter bean to handle this case.
at io.micronaut.data.runtime.mapper.ResultReader.lambda$convertRequired$0(ResultReader.java:51)
at java.base/java.util.Optional.orElseThrow(Optional.java:403)
at io.micronaut.data.runtime.mapper.ResultReader.convertRequired(ResultReader.java:50)
at io.micronaut.data.runtime.operations.internal.sql.AbstractSqlRepositoryOperations$4.map(AbstractSqlRepositoryOperations.java:827)
at io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations.findOne(DefaultJdbcRepositoryOperations.java:384)

Steps To Reproduce

No response

Environment Information

No response

Example Application

No response

Version

micronaut-data 4.x and later (probably earlier versions too)

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions