请求处理失败;嵌套异常是org.hibernate.TypeMismatchException:类java.lang.Long,得到类java.lang.String

时间:2016-09-20 09:53:27

标签: java spring hibernate jsp

当我在表单中输入数据时,它会生成此异常。在Java类中,id应该在数据库中的数据时自动生成。我发布了java类和jsp页面的代码

Task.java

package trascrizione.model;

@Entity
public class Task {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable=false)
    private String nome;

    @Column(nullable=false)
    private String descrizione;

    public Task() {

    }

    public Task(Long id,String nome, String descrizione) {
        super();
        this.nome = nome;
        this.id = id;
        this.descrizione = descrizione;

    }

    public Long getId() {
        return id;
    }


    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getDescrizione() {
        return descrizione;
    }

    public void setDescrizione(String descrizione) {
        this.descrizione = descrizione;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Task [id=" + id + ", nome=" + nome + ", descrizione=" + descrizione + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((descrizione == null) ? 0 : descrizione.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((nome == null) ? 0 : nome.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Task other = (Task) obj;
        if (descrizione == null) {
            if (other.descrizione != null)
                return false;
        } else if (!descrizione.equals(other.descrizione))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (nome == null) {
            if (other.nome != null)
                return false;
        } else if (!nome.equals(other.nome))
            return false;
        return true;
    }
}

ControllerTask.java

@Controller
public class ControllerTask {

    @Autowired
    private FacadeTask facadeTask;

    @RequestMapping(value="/inserisciTask")
    private String newTask(@ModelAttribute Task task) {
        return"amministrazione/inserisciTask";
    }

    @RequestMapping(value="/aggiungiTask", method = RequestMethod.POST)
    public String confermaTask(@ModelAttribute Task task, Model model, @Validated Task t, BindingResult bindingResult) {
        facadeTask.addTask(task);
        return "/homeAdmin";
    }

    @RequestMapping(value="/confermaTask", method = RequestMethod.POST)
    public String aggiungiTask(@ModelAttribute Task task, Model model, @Validated Task t,BindingResult bindingResult) {
        Task p = facadeTask.retriveTask(task.getNome());
        if(bindingResult.hasErrors()) {
            return "amministrazione/inserisciTask";
        }
        if(p!=null) {
            model.addAttribute("nomeErrore", "Nome esistente");
            return "amministrazione/inserisciTask";
        }
            model.addAttribute("task", task);
        return "amministrazione/inserisciTask";
    }

}

FacadeTask

@Service
public class FacadeTask {

    @Autowired
    private TaskDaoImpl taskDaoImpl;

    public void addTask(Task t) {
        taskDaoImpl.insertTask(t);
    }

    public Task retriveTask(String nome) {
        return this.taskDaoImpl.findTask(nome);
    }

    public List<Task> retriveAllTask() {
        return this.taskDaoImpl.findAll();
    }

}

TaskDaoImpl.java

@Repository
public class TaskDaoImpl implements TaskDao {

    @Autowired
    private SessionFactory sessionFactory;


    @Override
    public void insertTask(Task task) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(task);
        session.getTransaction().commit();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Task> findAll() {
        Session session = sessionFactory.openSession();
        String hql = "FROM Task";
        Query query = session.createQuery(hql);
        List<Task> empList = query.list();
        System.out.println("Task List:" + empList);
        return empList;
    }

    @Override
    public Task findTask(String nome) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        Task t = (Task) session.get(Task.class, nome);
        session.getTransaction().commit();
        return t;
    }

}

TaskDao.java

public interface TaskDao {

    public void insertTask(Task task);
    public List<Task> findAll();
    public Task findTask(String nome);

}

inserisciTask.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page session="true"%>
<%@ taglib uri="http://www.springframework.org/tags/form"
    prefix="springForm"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Trascrizioni Medievali</title>
</head>
<body>

    <h1>
        <center>Inserisci un nuovo Task</center>
    </h1>

    <div align="center">

    <form:form method="post" action="confermaTask" modelAttribute="task" name="form">

        <td>Nome : </td>
        <td> <form:input type="text" path="nome" placeholder="nome"/></td><br>

        <td>Descrizione : </td>
        <td> <form:input type="text" path="descrizione" placeholder="descrizione"/></td><br>

        <font size="3" color="red"><springForm:errors path="nome" cssClass="error" /></font><br/>
            <font size="3" color="red"><springForm:errors path="descrizione" cssClass="error" /></font><br/>

        <td><input type="submit" value="invia" /></td>
        </form:form>

        <br>

        <c:if test="${not empty task.nome }">

        <p><c:out value="Conferma la seguente informazione:"/><p>
        <b><p><c:out value="Nome"/></b> ${task.nome}<br>
        <b><p><c:out value="Descrizione"/></b> ${task.descrizione}<br>

        <form:form method="post" action="aggiungiTask" modelAttribute="task" name="form2">

        <form:input type="hidden" path="descrizione" placeholder="nome"/><br>

        <form:input type="hidden" path="descrizione" placeholder="descrizione"/><br>

        <td><input type="submit" value="Conferma" /></td>

        </form:form>


        </c:if>




    </div>

</body>
</html>

0 个答案:

没有答案