I'm new to spring r2dbc. Previously I've user hibernate. In hibernate when you need to map postgresql enum to java enum you just add com.vladmihalcea:hibernate-types-52
and use @Enumerated
(as shown bellow). Related to R2DBC and enum (PostgreSQL) SO question I have to create codec for every single enum. Is it possible to achive this with some kind of tag or other general solution not just creating multiple codecs.
CREATE TYPE user_type_enum AS ENUM ('ADMIN', 'USER');
public class PostgreSQLEnumType extends org.hibernate.type.EnumType {
public void nullSafeSet(PreparedStatement st, Object value,
int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException {
st.setObject(
index,
value != null ? ((Enum) value).name() : null,
Types.OTHER
);
}
}
public enum UserTypeEnum {
ADMIN,
USER
}
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Table;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
@Table;
@TypeDef(
name = "pgsql_enum",
typeClass = PostgreSQLEnumType.class
)
public class User {
@Id
private Long id;
private String usename;
@Enumerated(EnumType.STRING)
@Type(type = "pgsql_enum")
private UserEnumType userType;
// Getters and setters provided
}