在hibernate中获取ClassCastException

时间:2012-09-16 17:07:13

标签: java hibernate

我正在hibernate中编写一些代码,在我的表(hall_block_calendar)中插入一行之前,我需要更新其他一些表(hall_calendar)。 我有状态F,E,M,只有那些具有“F”的表格提交的日期才被标记为“N”。我的pojo类Hall_calendar.java的映射文件包含hall_code和calendar_date的复合键以及pojo类中的hallbookingcompid。

我的查询工作正常。但是,for循环抛出异常。我将不胜感激。

        public boolean addHallCalendarBlock(Hall_block_calendar hbc, Hall_calendar hc)
{
   //boolean result;
   Session session=HibernateUtil.getSessionFactory().openSession();   
   Transaction tx=null;
   try
    {
       tx=session.beginTransaction();

       Query q=session.createQuery("from Hall_calendar h where h.hallbookingcompid.calendar_date between'"+hbc.getHall_block_from_date()+"' and '"+hbc.getHall_block_to_date()+"' and hall_availability='F'");

       System.out.println("query working: "+q.list());

       if(!q.list().isEmpty())
       {   
           Hall_calendar upd_obj = new Hall_calendar();
           for(Iterator it=q.iterate();it.hasNext();)
           {   


             Object[] row = (Object[]) it.next();
             System.out.println("row[0]: "+(String)row[0]);
             upd_obj= (Hall_calendar) session.load(Hall_calendar.class, (String)row[0]);

             upd_obj.setHall_availability("N"); 
             session.save(upd_obj); 
           }

           session.save(hbc);
           tx.commit();  
       }   
    }
   catch(Exception e)
   {
       tx.rollback();
       e.printStackTrace();
       return false;
   }
   finally
   {
       session.close();
   }
   return true;
}


query working: [com.cmc.sibs.vo.Hall_calendar@7e9bed, com.cmc.sibs.vo.Hall_calendar@4d2125, com.cmc.sibs.vo.Hall_calendar@1bb41d7, com.cmc.sibs.vo.Hall_calendar@df9252]
java.lang.ClassCastException: com.cmc.sibs.vo.Hall_calendar
    at com.cmc.sibs.dao.SibsDao.addHallCalendarBlock(SibsDao.java:21861)
    at com.cmc.sibs.delegates.SibsDelegate.addHallCalendarBlock(SibsDelegate.java:2982)
    at com.cmc.sibs.servlets.AddHallBlocking.doPost(AddHallBlocking.java:86)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
boolean in servlet: false

2 个答案:

答案 0 :(得分:3)

首先,您违反了所有Java代码标准。您基本上是用Java编写c ++代码。其次,我不明白你为什么要按照你的方式进行迭代。当你在没有select子句的查询中执行query.list()时,你将获得完整的对象。为什么不这样做:

final List<Hall_calendar> res = (List<Hall_calendar>) q.list();
for (final Hall_calendar hall_calendar : res) {
    hall_calendar.setHall_availability("N");
    //no need to explicitly save these..
}
session.save(hbc);

答案 1 :(得分:0)

您使用&#34;查询&#34;接口,它没有iterator()方法。使用list()方法将一个列表存储查询结果带到该列表,您可以按照以下方式进行for循环。

for(class_name object : list_name)
{
//Performed action using object
}