[java.lang.ArrayIndexOutOfBoundsException:索引 2 超出长度 2 的范围]
[java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2]
我知道有人问过这个问题 but I can't found a solution. So the 在那个问题中解释问题:
The exception happens at EnumJavaTypeDescriptor.fromOrdinal. So my
guess is that your role_id value in the database column is 2.
But there are only two values in the UserType enum: ADMIN and USER.
ADMIN's ordinal is 0, and USER's ordinal is 1. So if your row contains
2 in the user_type column, it's an invalid ordinal for the UserType
enum.
在数据库中,我有两个值 1 和 2,代表两个字符串“black”和“white”,所以我不能将它们从 1 和 2 更改为 0 和 1。所以我应该在我的 java 代码,所以我尝试了这个,但我一直收到同样的错误
public enum TypeColor {
black(1),
white(2);
private int val;
private TypeColor(int val) {
this.val = val;
}
}
更新
这是使用该枚举的实体
@Entity
@Table(name = "BOOKS")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@Column(name = "id")
private Long id;
private TypeColor typeColor;
//other attributes, getters, setters
}
当我尝试获取所有书籍时在 Controller 中出现错误(在标题中)
@GetMapping("/books")
public ResponseEntity<List<Book>> getAllBooks(@org.springdoc.api.annotations.ParameterObject Pageable pageable) {
log.debug("REST request to get a page of Books");
Page<Rdv> page = bookRepository.findAll(pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
return ResponseEntity.ok().headers(headers).body(page.getContent());
}
要将 int 转换为枚举,其中 int 不是枚举常量的 ordinal(),您需要一个查找函数。您已经将整数值分配给常量,所以剩下的就是查找本身:
public enum TypeColor {
black(1),
white(2);
private int val;
private TypeColor(int val) {
this.val = val;
}
static TypeColor lookup(int v) {
for(TypeColor t:values()) {
if(t.val == v) {
return t;
}
}
return null; //or throw an Exception
}
}
好吧,我必须获得更多关于枚举世界的文档。如果有人遇到与我相同的问题,这里是对我有用的解决方案:
这个link对我帮助很大
TypeColor.java
public enum TypeColor {
black("1"), //in database this field has a varchar as datatype
white("2");
private String code;
private TypeColor(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
所以我需要的缺失部分是以下 class,它使用 JPA 2.1
在数据库中的列和枚举之间建立映射
TypeColorConverter.java
@Converter(autoApply = true)
public class TypeColorConverter implements AttributeConverter<TypeColor, String> {
@Override
public String convertToDatabaseColumn(TypeColor color) {
if (color == null) {
return null;
}
return color.getCode();
}
@Override
public TypeColor convertToEntityAttribute(String code) {
if (code == null) {
return null;
}
return Stream
.of(TypeColor.values())
.filter(c -> c.getCode().equals(code))
.findFirst()
.orElseThrow(IllegalArgumentException::new);
}
}
我知道有人问过这个问题
The exception happens at EnumJavaTypeDescriptor.fromOrdinal. So my guess is that your role_id value in the database column is 2.
But there are only two values in the UserType enum: ADMIN and USER. ADMIN's ordinal is 0, and USER's ordinal is 1. So if your row contains 2 in the user_type column, it's an invalid ordinal for the UserType enum.
在数据库中,我有两个值 1 和 2,代表两个字符串“black”和“white”,所以我不能将它们从 1 和 2 更改为 0 和 1。所以我应该在我的 java 代码,所以我尝试了这个,但我一直收到同样的错误
public enum TypeColor {
black(1),
white(2);
private int val;
private TypeColor(int val) {
this.val = val;
}
}
更新
这是使用该枚举的实体
@Entity
@Table(name = "BOOKS")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
@Column(name = "id")
private Long id;
private TypeColor typeColor;
//other attributes, getters, setters
}
当我尝试获取所有书籍时在 Controller 中出现错误(在标题中)
@GetMapping("/books")
public ResponseEntity<List<Book>> getAllBooks(@org.springdoc.api.annotations.ParameterObject Pageable pageable) {
log.debug("REST request to get a page of Books");
Page<Rdv> page = bookRepository.findAll(pageable);
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
return ResponseEntity.ok().headers(headers).body(page.getContent());
}
要将 int 转换为枚举,其中 int 不是枚举常量的 ordinal(),您需要一个查找函数。您已经将整数值分配给常量,所以剩下的就是查找本身:
public enum TypeColor {
black(1),
white(2);
private int val;
private TypeColor(int val) {
this.val = val;
}
static TypeColor lookup(int v) {
for(TypeColor t:values()) {
if(t.val == v) {
return t;
}
}
return null; //or throw an Exception
}
}
好吧,我必须获得更多关于枚举世界的文档。如果有人遇到与我相同的问题,这里是对我有用的解决方案:
这个link对我帮助很大
TypeColor.java
public enum TypeColor {
black("1"), //in database this field has a varchar as datatype
white("2");
private String code;
private TypeColor(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
所以我需要的缺失部分是以下 class,它使用 JPA 2.1
在数据库中的列和枚举之间建立映射TypeColorConverter.java
@Converter(autoApply = true)
public class TypeColorConverter implements AttributeConverter<TypeColor, String> {
@Override
public String convertToDatabaseColumn(TypeColor color) {
if (color == null) {
return null;
}
return color.getCode();
}
@Override
public TypeColor convertToEntityAttribute(String code) {
if (code == null) {
return null;
}
return Stream
.of(TypeColor.values())
.filter(c -> c.getCode().equals(code))
.findFirst()
.orElseThrow(IllegalArgumentException::new);
}
}