Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring InvalidDataAccessApiUsageException:没有枚举常量_Spring_Spring Boot_Jpa_Spring Data Jpa_H2 - Fatal编程技术网

Spring InvalidDataAccessApiUsageException:没有枚举常量

Spring InvalidDataAccessApiUsageException:没有枚举常量,spring,spring-boot,jpa,spring-data-jpa,h2,Spring,Spring Boot,Jpa,Spring Data Jpa,H2,我有一个角色enum,如下所示: public enum Role{ admin('a'), member('m'), pending('p'); char role; Role(char a) { this.role = a; } public char getRole() { return role; } public static Role getByRole(char role) {

我有一个角色enum,如下所示:

public enum Role{
    admin('a'),
    member('m'),
    pending('p');
    char role;
    Role(char a) {
        this.role = a;
    }
    public char getRole() {
        return role;
    }
    public static Role getByRole(char role) {
        return Arrays.stream(Role.values())
                .filter(Role -> Role.getRole() == role)
                .findFirst()
                .orElse(Role.pending);
    }
}
为了支持转换,我创建了一个名为RoleConverter的类:

@Converter
public class RoleConverter implements AttributeConverter<Role, Character> {
    @Override
    public Character convertToDatabaseColumn(Role Role) {
        return Role.getRole();
    }
    @Override
    public Role convertToEntityAttribute(Character dbData) {
        System.out.println(dbData);
        return Role.getByRole(dbData);
    }
}
尽管如此,它还是给了我错误-嵌套异常是org.springframework.dao.InvalidDataAccessApiUsageException:No enum constant com.mua.cse616.model.Role.2


将spring与h2和jpa一起使用,您的数据库包含一个role=2的条目


确保数据库中的条目具有与枚举中相同的值。

数据库中似乎有一行在列中具有值2,而该值显然不在枚举中。可能您一开始没有使用
@Enumerated
注释,因此JPA使用序号作为列值。

似乎您的数据库中有一行,列中有值2,而枚举中没有obv。也许你一开始没有@Enumerated注解,因此JPA使用序号作为列值。@RobertNiestroj我删除了旧的db,现在一切正常,你能把它作为答案发布吗?
    @Convert(converter = RoleConverter.class)
    @Enumerated(EnumType.STRING)
    public Role role;