gpt4 book ai didi

java - SqlExceptionHelper.logExceptions int 类型的错误值 : WRAP

转载 作者:行者123 更新时间:2023-12-01 22:09:29 28 4
gpt4 key购买 nike

您好,我正在关注《行动》第 5 版书中的 spring,当我想从成分表中获取参数时遇到问题。在以下方法中,我尝试使用成分Repository.findAll() 填充列表:

@GetMapping
public String showDesignForm(Model model){
List<Ingredient> ingredients = new ArrayList<>();
ingredientRepository.findAll().forEach(ingredients::add);

Type[] types = Ingredient.Type.values();
for (Type type : types){
model.addAttribute(type.toString().toLowerCase(),
filterByType(ingredients, type));
}
model.addAttribute("taco", new Taco());
return "welcomePage";
}

但发现以下错误:

org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute query; SQL [select ingredient0_.id as id1_0_, ingredient0_.name as name2_0_, ingredient0_.type as type3_0_ from Ingredient ingredient0_]; nested exception is org.hibernate.exception.DataException: could not execute query] with root cause
org.postgresql.util.PSQLException: Bad value for type int : WRAP

这就是我的成分实体的外观:

@Data
@RequiredArgsConstructor
@NoArgsConstructor(access=AccessLevel.PRIVATE, force=true)
@Entity
public class Ingredient {
@Id
@Column(name = "id", columnDefinition = "varchar(4)")
private final String id;
@Column(columnDefinition = "varchar(25)")
private final String name;
@Column(columnDefinition = "varchar")
private final Type type;
public static enum Type {
WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
}
}

我该如何解决这个问题?提前致谢

最佳答案

使用 JPA 的枚举可以通过 @Enumerated 注释使用不同的获取方法。

默认情况下,这些值似乎是通过序数值获取的,这就是您收到此错误的原因,因为您将值存储为 string 。

尝试以下操作,告诉 JPA 您的枚举值将保留为 String :

@Column(columnDefinition = "varchar")
@Enumerated(EnumType.STRING)
private final Type type;

关于java - SqlExceptionHelper.logExceptions int 类型的错误值 : WRAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60098367/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com