how to store enum as tinyint in mysql with r2dbc

Viewed 29

I am working with Spring WebFlux and r2dbc.

I have an entity with an enum field, and I would like to store it as tinyint in mysql while serializing as string.

// Order.java

@Data
@NoArgsConstructor
@AllArgsConstructor
@Table("orders")
public class Order {
    @Id
    Long id;

    StatusEnum status;
}

// StatusEnum.java

@AllArgsConstructor
public enum StatusEnum {
    PENDING(11),
    IN_PROGRESS(12),
    SUCCESSFUL(13),
    FAILED(14),

    UNKNOWN(255);

    final int value;
}

// sql

CREATE TABLE IF NOT EXISTS orders (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    status TINYINT UNSIGNED NOT NULL DEFAULT 255
)  ENGINE=INNODB;

// API result for e.g. GET /api/orders

[
    { "id": 1, "status": "PENDING" },
    { "id": 2, "status": "IN_PROGRESS" },
    { "id": 3, "status": "SUCCESSFUL" },
    { "id": 4, "status": "FAILED" }
]

It reported error when inserting records:

Incorrect integer value: 'PENDING' for column 'status' at row 1; nested exception is io.r2dbc.spi.R2dbcNonTransientResourceException

How can I resolve it? Thanks.

0 Answers
Related