有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当字段为LOB时,java无法从字符串转换为Long类型

在spring MVC应用程序中,我有一个问题实体类

@Entity
public class Question {

    @Lob
    @Column(name="QUESTION_TITLE")
    private String question;
...
}

我用百里香。对于这个领域,我的观点如下

<input type="text" class="form-control" id="question" 
th:field="*{question}" th:value="${question}" placeholder="">

我的控制器保存方法是

@PostMapping("/save")
    public String saveQuestion(Question question, BindingResult bindingResult){
        questionService.save(question);

        return "redirect:/admin/questions/all/";
    }

但当我提交时,我犯了错误

Failed to bind request element: 
org.springframework.beans.TypeMismatchException:Failed to convert value of type 'java.lang.String' to required type com.sendit.security.model.Question'; 

nested exception is 
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'what';

nested exception is java.lang.NumberFormatException:
For input string: "what"

当我向问题字段添加@Convert(Convert=QuestionConverter.class)属性并实现类似于bellow的QuestionConverter方法时

@Converter
public static class QuestionConverter implements AttributeConverter<String, Integer> {

    @Override
    public Integer convertToDatabaseColumn(String attribute) {
        return attribute.length();
    }

    @Override
    public String convertToEntityAttribute(Integer dbData) {
        return "";
    }
}

我又犯了错误


共 (1) 个答案

  1. # 1 楼答案

    我找到了解决办法。只需将“问题”字段名更改为另一个字段名(如“标题”)即可。这是因为在视图中,当我想发送“question”属性时,它与object和sent object散列值类似