java - org.hibernate.TypeMismatchException : Provided id of the wrong type while fetching data

标签 java spring hibernate

我试图通过使用关键字查询(以“类(class)代码”的形式绑定(bind))从数据库表“问题”中获取一些搜索结果。目标是从具有外键“Course_coursecode”(字符串值)的列中获取表中的结果。这是我的代码片段:

    @Transactional
            @RequestMapping(value = "/question-list", method = RequestMethod.GET)
            public ModelAndView viewQuestionList(@RequestParam("course-code")String code, ModelAndView model){

                    model.setViewName("question-list");

                    Question question = em.find(com.databaseproject.questor.model.Question.class, code);
                    model.addObject("question", question);

                    List<Question> questions = 

(List<Question>)em.createQuery("SELECT q FROM Question q WHERE q.Course_coursecode =: code")
                        .setParameter("code", code).getResultList();

                model.addObject("questionList", questions);

                return model;
        }

现在,当我使用“CSE137”形式的字符串进行搜索时,我得到:

org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.databaseproject.questor.model.Question. Expected: class java.lang.Integer, got class java.lang.String

java.lang.IllegalArgumentException: Provided id of the wrong type for class com.databaseproject.questor.model.Question. Expected: class java.lang.Integer, got class java.lang.String

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class com.databaseproject.questor.model.Question. Expected: class java.lang.Integer, got class java.lang.String

有趣的是,当我输入 int 值作为搜索查询时,我得到:

org.hibernate.TypeMismatchException: Provided id of the wrong type for class com.databaseproject.questor.model.Question. Expected: class java.lang.Integer, got class java.lang.String

java.lang.IllegalArgumentException: Provided id of the wrong type for class com.databaseproject.questor.model.Question. Expected: class java.lang.Integer, got class java.lang.String

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Provided id of the wrong type for class com.databaseproject.questor.model.Question. Expected: class java.lang.Integer, got class java.lang.String

我在这里做错了什么?我该如何修复它?

更新: 这是我的实体类-

package com.databaseproject.questor.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;

@Entity
public class Question {

    @Id
    private int idQuestion;
    private String questionText;
    private String solutionText;
    private byte[] image;
    private String filepath;
    private int year;
    private String User_username;
    private int Teacher_idTeacher;
    private String Course_coursecode;

    @Transient
    private String encodedImage;

    public int getIdQuestion() {
        return idQuestion;
    }

    public void setIdQuestion(int idQuestion) {
        this.idQuestion = idQuestion;
    }

    public String getQuestionText() {
        return questionText;
    }

    public void setQuestionText(String questionText) {
        this.questionText = questionText;
    }

    public String getSolutionText() {
        return solutionText;
    }

    public void setSolutionText(String solutionText) {
        this.solutionText = solutionText;
    }

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    public String getFilepath() {
        return filepath;
    }

    public void setFilepath(String filepath) {
        this.filepath = filepath;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getUser_username() {
        return User_username;
    }

    public void setUser_username(String user_username) {
        User_username = user_username;
    }

    public int getTeacher_idTeacher() {
        return Teacher_idTeacher;
    }

    public void setTeacher_idTeacher(int teacher_idTeacher) {
        Teacher_idTeacher = teacher_idTeacher;
    }

    public String getCourse_coursecode() {
        return Course_coursecode;
    }

    public void setCourse_coursecode(String course_coursecode) {
        Course_coursecode = course_coursecode;
    }

    public String getEncodedImage() {
        return encodedImage;
    }

    public void setEncodedImage(String encodedImage) {
        this.encodedImage = encodedImage;
    }
}

最佳答案

观察实体,正如 @JB Nizet 所指出的,您正在尝试通过 id 查找问题发生

Question question = em.find(com.databaseproject.questor.model.Question.class, code);

传递一个字符串“代码”。如果“code”代表 id,您应该将其转换为 int:

model.setViewName("question-list");
int id = 0;
try
{
    if(code != null)
      id = Integer.parseInt(code);
}
catch (NumberFormatException e)
{
    id = 0;
}
Question question = em.find(com.databaseproject.questor.model.Question.class, id);

还处理可能未找到的问题。如果“code”不是 id,您应该进行查询(使用条件、native 或 jpql)来获取问题实体列表,选择您需要的实体。

编辑 -> 获取问题实体列表:

TypedQuery<Question> query = em.createQuery("SELECT q FROM Question q WHERE q.Course_coursecode = :coursecode", Question.class);
query.setParameter("coursecode", code);
List<Question> results = query.getResultList();

关于java - org.hibernate.TypeMismatchException : Provided id of the wrong type while fetching data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36378482/

相关文章:

java - 将 MySQL 几何图形映射到 EJB 实体时出错

java - jackson kotlin - 无法从 START_OBJECT token 中反序列化 `java.util.ArrayList` 的实例

java - Selenium显式等待设置间隔时间

java - Spring Security - 具有 IS_AUTHENTICATED_ANONYMOUSLY 但仍指定(且未知)凭据的 Url 被框架阻止

java - 带注释的 Spring + Hibernate : how to bind Hibernate Session to thread?

java - 我正在尝试从百思买检索商品名称和价格,如何让它在第一个价格之后停止?

jquery - 对 Controller 的 ajax POST 调用在 spring 中给出 null 值

java - 将 Scala Future 转换为 Reactor Flux

postgresql - macOS 中的 Postgres 和 JPA

java - 当添加新实体时,Hibernate 可在服务器上运行,但不能在 Eclipse 中运行