Mai*_*sad 3 spring jpa h2 spring-data-jpa spring-boot
我有一个 Role 枚举,如下所示:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
为了支持转换,我创建了一个名为 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);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的 Target 对象中,我添加了适当的注释:
@Convert(converter = RoleConverter.class)
@Enumerated(EnumType.STRING)
public Role role;
Run Code Online (Sandbox Code Playgroud)
它仍然给我错误 -嵌套异常是 org.springframework.dao.InvalidDataAccessApiUsageException: No enum constant com.mua.cse616.model.Role.2;
在 h2 和 jpa 中使用 spring
| 归档时间: |
|
| 查看次数: |
4440 次 |
| 最近记录: |