2014-07-25 10 views
-1

데이터베이스에서 개체를 검색하기 만하면 쿼리가 실행되지만 예외는 발생합니다.데이터베이스에서 개체를 가져올 수 없습니다.

@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) 
+0

왜 새 Tache()입니까? 그것은 말이되지 않습니다. 'Tache'는 최대 절전 모드로 관리되는 POJO입니까? – Bart

답변

0

당신이 정말로 클래스 Taché에를 초기화해야합니까?

방금 ​​다음과 같이 말하면 작동합니까?

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

코딩 할 필요가 없습니다.

Tache tache = new Tache();

+1

여전히 동일한 예외가 있습니다 – Ouda

+1

Hibernate에서 Native SQL을 사용한다고 가정하지만 HQL과 다소 다릅니다. SQL 쿼리에서 idtache를 사용하고 있지만 'idtache'가 매개 변수로 설정되어 있지 않습니다. 또한 쿼리에 클래스를 추가하는 것이 좋습니다. . @Override 공공 Taché에 getLastSousTache (긴 myTacheid) { 세션 세션 = HibernateUtil.getSessionFactory()과 getCurrentSession()에(); 네이티브 SQL없이 – Davy

+0

이 같은 뭔가 session.beginTransaction(); Tache tache = null; String queryString = "from Tache id = : id"; 쿼리 쿼리 = session.createQuery (queryString); query.setLong ("id", myTacheid); tache = (Tache) query.uniqueResult(); return tache; } 당신은 sqlquery가 약간 다르므로 이것을 살펴 봐야합니다. – Davy

관련 문제