小编aan*_*g13的帖子

在 Spring 实体中使用枚举

您应该如何设置使用 Enum 的 Spring 实体?

我已经建立了一个 spring-boot 项目并提供了下面的代码,希望有人能告诉我正确的方法。

我有一个 Spring 实体设置

    @Entity
    public class Resource {

    @Id
    private String id;
    private String name;
    @Column(name="resourceType")
    private ResourceType type;
    //Getters and setters}
Run Code Online (Sandbox Code Playgroud)

我已经设置了实体中使用的枚举

    public enum ResourceType {
    financial("financial"),
    raw("raw"),
    factory("factory"),
    lab("lab");

    String value;

    ResourceType(String value) {
        this.value =value;
    }
    }
Run Code Online (Sandbox Code Playgroud)

我正在使用 MySQL 并已设置表并添加数据

    create table resource
    (
    id           varchar(10) not null,
    name         varchar(30) not null,
    resourceType ENUM('financial','raw','factory','lab')
    );
    insert into resource(id, name, resourceType)
    values 
       ('O', 'Oxygen', 'raw');

Run Code Online (Sandbox Code Playgroud)

如果我运行 Spring 应用程序并尝试返回所有实体的列表,就会出现问题。

2021-06-03 17:33:16.436 …
Run Code Online (Sandbox Code Playgroud)

java mysql spring spring-data-jpa spring-boot

9
推荐指数
1
解决办法
3万
查看次数

标签 统计

java ×1

mysql ×1

spring ×1

spring-boot ×1

spring-data-jpa ×1