Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bindProperties throws error when entities contain enum fields #854

Closed
detomarco opened this issue Feb 14, 2024 · 1 comment
Closed

bindProperties throws error when entities contain enum fields #854

detomarco opened this issue Feb 14, 2024 · 1 comment
Labels
for: external-project For an external project and not something we can fix

Comments

@detomarco
Copy link

detomarco commented Feb 14, 2024

It seems like entities cannot have enum fields when saving using bindProperties or bind APIs

Spring boot 3.2.2

@Table("cars")
public class Car {
    @Id
    private UUID id;
    private String name;
    private Language language;
   
   // getters - setters
}

public enum Language {
    EN, NL
}

@Repository
public class CarRepository {

    private final DatabaseClient databaseClient;

    public CarRepository(DatabaseClient databaseClient) {
        this.databaseClient = databaseClient;
    }

    public Car save(Car car) {

        databaseClient.sql("INSERT INTO cars(id,name, language) VALUES(:id,:name, :language)")
                .bindProperties(car)
                .fetch()
                .one()
                .block();

        return car;

    }
}

@Test
void contextLoads() {
    Car car = new Car();
    car.setId(UUID.randomUUID());
    car.setName("BMW");
    car.setLanguage(Language.EN);

    carRepository.save(car);

}

This test throws this exception

java.lang.IllegalArgumentException: Cannot encode parameter of type io.r2dbc.spi.Parameters$InParameter (EN)
	at io.r2dbc.postgresql.codec.DefaultCodecs.encodeParameterValue(DefaultCodecs.java:290)
	at io.r2dbc.postgresql.codec.DefaultCodecs.encode(DefaultCodecs.java:264)
	at io.r2dbc.postgresql.PostgresqlStatement.bind(PostgresqlStatement.java:108)
	at io.r2dbc.postgresql.PostgresqlStatement.bind(PostgresqlStatement.java:59)

Would it be possible to support enumeration in bindProperties api?

Full code here

@detomarco detomarco changed the title bindProperties throws error when entities contain enum bindProperties throws error when entities contain enum fields Feb 14, 2024
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 14, 2024
@mp911de
Copy link
Member

mp911de commented Feb 20, 2024

Thanks for reaching out. DatabaseClient is part of Spring Framework. It isn't opinionated about enum representations (ordinal vs. name) therefore the value is passed on directly as-is.

I suggest reaching out to Spring Framework and filing a ticket for discussion.

Furthermore, you can use Postgres Enum Types to serialize Java enums to Postgres enums if you wish. However, that requires a bit of driver configuration.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Feb 20, 2024
@mp911de mp911de added for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

3 participants