Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.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
Java 使用Spring数据JPA获取NumberFormatException_Java_Spring_Jpa_Numberformatexception - Fatal编程技术网

Java 使用Spring数据JPA获取NumberFormatException

Java 使用Spring数据JPA获取NumberFormatException,java,spring,jpa,numberformatexception,Java,Spring,Jpa,Numberformatexception,我正在用“Spring in action 5”学习Spring,遇到了一些问题:当我在第3章从JDBC(100%工作正常)切换到Spring数据JPA时,代码停止工作,因为我试图打开包含taco成分的主页。我做了一些日志来查看发生了什么,发现findById(stringid)方法无法从DB(或类似的东西)转换我的值。我正在使用MySQL 我尝试自己使用@Autowired调用convertor的方法convert(stringid),但我唯一发现的是,当key错误时,会出现另一个错误。因此数

我正在用“Spring in action 5”学习Spring,遇到了一些问题:当我在第3章从JDBC(100%工作正常)切换到Spring数据JPA时,代码停止工作,因为我试图打开包含taco成分的主页。我做了一些日志来查看发生了什么,发现findById(stringid)方法无法从DB(或类似的东西)转换我的值。我正在使用MySQL

我尝试自己使用@Autowired调用convertor的方法convert(stringid),但我唯一发现的是,当key错误时,会出现另一个错误。因此数据是可见的。我将尝试在这里提供一些代码,但我不确定什么有用,什么不有用。我在第一次尝试记录某些内容时出错。IDE和浏览器中的错误是不同的。 这是完整的项目

这是我的转换器:

    public class IngredientByIdConverter implements Converter<String, 
    Ingredient> {

    private IngredientRepository ingredientRepo;

    @Autowired
    public IngredientByIdConverter(IngredientRepository ingredientRepo) {
        this.ingredientRepo = ingredientRepo;

    }
    @Override
        public Ingredient convert(String id) {
            log.info("In converter.convert(): " 
        +ingredientRepo.findById(id).toString());
            Optional<Ingredient> optionalIngredient = 
        ingredientRepo.findById(id);
            return optionalIngredient.orElse(null);
        }
    }
公共类IngCreditByIDConverter实现转换器{
私人IngredEntrepository IngredEnterpo;
@自动连线
公共InCreditByIDConverter(IngredEnterpository IngredEnterpo){
this.IngredEnterpo=IngredEnterpo;
}
@凌驾
公共成分转换(字符串id){
log.info(“在converter.convert()中:”
+ingredientpo.findById(id.toString());
可选选项要素=
IngredEnterpo.findById(id);
返回optionalintent.orElse(空);
}
}
和控制器类:

    @Slf4j
    @Controller
    @RequestMapping("/design")
    @SessionAttributes("order")
    public class DesignTacoController {

    @ModelAttribute(name = "order")
    public Order order() {
        return new Order();
    }

    @ModelAttribute(name = "taco")
    public Taco taco() {
        return new Taco();
    }

    private final IngredientRepository ingredientRepository;
    private TacoRepository designRepository;
    private IngredientByIdConverter converter;

    @Autowired
    public DesignTacoController(IngredientRepository ingredientRepository,
                                TacoRepository designRepository,
                                IngredientByIdConverter converter) {
        this.ingredientRepository = ingredientRepository;
        this.designRepository = designRepository;
        this.converter = converter;
    }

    @GetMapping
    public String showDesignForm(Model model) {
        List<Ingredient> ingredients = new ArrayList<>();



        log.info(converter.convert("CARN").getName());
        log.info("in DTC: " + ingredientRepository.findAll());

        ingredientRepository.findAll().forEach(i -> ingredients.add(i));
@Slf4j
@控制器
@请求映射(“/design”)
@会期贡献(“命令”)
公共类设计控制器{
@ModelAttribute(name=“order”)
公共秩序(){
返回新订单();
}
@ModelAttribute(name=“taco”)
公共塔可塔可{
返回新的玉米卷();
}
私人最终存款存款存款存款存款存款存款存款存款存款;
私有存储库;
专用IngreditById转换器;
@自动连线
公共设计塔科控制员(IngredEnterpository IngredEnterpository,
TacoRepository设计存储库,
InCreditById转换器(转换器){
this.IngredEnterpository=IngredEnterpository;
this.designRepository=designRepository;
这个。转换器=转换器;
}
@GetMapping
公共字符串showDesignForm(模型){
列表成分=新的ArrayList();
log.info(converter.convert(“CARN”).getName());
log.info(“在DTC中:+IngredEntrepository.findAll());
findAll().forEach(i->components.add(i));
在IDE中:

java.lang.NumberFormatException:用于输入字符串:“蛋白质” 在java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)~[na:na] 在java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)~[na:na] 在java.base/java.lang.Double.parseDouble(Double.java:543)~[na:na]

在浏览器中:

出现意外错误(类型=内部服务器错误,状态=500)。 对于输入字符串:“蛋白质”,嵌套异常为java.lang.NumberFormatException:对于输入字符串:“蛋白质” org.springframework.dao.InvalidDataAccessApiUsageException:用于输入字符串:“蛋白质”;嵌套异常为java.lang.NumberFormatException:用于输入字符串:“蛋白质” 在org.springframework.orm.jpa.EntityManagerFactoryUtils.ConvertJPAAccessExceptionIffailable(EntityManagerFactoryUtils.java:373)上 在org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateException如果可能的话(HibernateJpaDialect.java:255) 位于org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.TranslateExceptionIffailable(AbstractEntityManagerFactoryBean.java:527) 在org.springframework.dao.support.ChainedPersistenceExceptionTranslator.TranslateExceptionIfEnabled(ChainedPersistenceExceptionTranslator.java:61) 位于org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) 位于org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
在org.springframework.aop.framework.ReflectiveMethodInvocation.procedue(ReflectiveMethodInvocation.java:186)

中,对于配料,java需要为字段
类型添加注释@Enumerated(EnumType.STRING)

@Id
@NaturalId(mutable = false)
private final String id;
private final String name;
@Enumerated(EnumType.STRING)
private final Type type;

public static enum Type {
    WRAP, PROTEIN, VEGGIES, CHEESE, SAUCE
}

@帕维尔的解决方案确实有效

我只想指出,还有一件事,可能值得一查

这本书讨论了JPA之前的JDBC,它使用data.sql将数据插入到表“component”中。在data.sql文件中,“component.type”的类型是字符串,其值包含“WRAP”、“PROTEIN”等

但是,在JPA的示例中,data.sql的内容被移动到TacoCloudApplication,该应用程序在方法“dataLoader”中写入,并且在该方法中,您可以看到它只创建了具有component.Type(而不是字符串)的component实例

要找出这两种方法之间的差异,可以运行TacoCloudApplication,然后查找“配料”表


如果您使用JDBC版本示例代码,则类型字段的值为字符串。如果您使用JPA版本示例代码,则类型字段的值为整数。

根据您的实体定义,可能缺少@Enumerated annotation:@Compass,非常感谢!现在工作正常。但是我可以问一下此异常的性质以及发生了什么情况吗n幕后?通常,您通过映射值存储枚举以节省数据库上的空间,即0,1,2,3,4。这允许您在不破坏数据库的情况下修改实际枚举显示值。如果只是为了练习,您可以使用字符串值,但如果蛋白质突然变为前端的蛋白质,则必须在t上映射蛋白质->蛋白质后端比较混乱。@Enumerated基本上是以字符串值作为面值。