无法从数据库中获取对象

时间:2014-07-25 12:34:58

标签: mysql sql hibernate spring-mvc

我只是想从数据库中检索一个实现我的查询的对象,但我得到了例外。

@Override
public Tache getLastSousTache() {
    Session session =  HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    String sql = "select * from tache where idtache=(select max(idTache) from tache where soustache is not null)";
    Query query = session.createSQLQuery(sql);
    Tache tache = new Tache();
    tache = (Tache) query.uniqueResult();
    return tache;
}

我遇到了这个例外:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.model.Tache
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

1 个答案:

答案 0 :(得分:0)

你真的需要初始化你的课程Tache吗?

如果您只是说:

,它是否有效

Tache tache = null; tache =(Tache)query.uniqueResult();

所以没有编码:

Tache tache = new Tache();