Reputation: 569
I'm writing some code in hibernate where I'm required to update some other table (hall_calendar) before a row is inserted in my table (hall_block_calendar). I have status F, E, M from which only those dates submitted by the form having "F" are to be marked "N". My mapping file for pojo class Hall_calendar.java contains composite key for hall_code and calendar_date and hallbookingcompid in the pojo class.
My query is working fine. However, the for loop throws an exception. I would appreciate any inputs on this.
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
Upvotes: 0
Views: 282
Reputation: 4807
You use "Query" interface and it has not iterator() method. Take one list store query result to that list using list() method and u can make a for loop in following way.
for(class_name object : list_name)
{
//Performed action using object
}
Upvotes: 0
Reputation: 12205
First off, you're violating every Java code standard. You are basically writing c++ code with Java. Secondly, I do not get why you iterate the way you do. When you do a query.list(), on a query without a select clause, you will get back full objects. Why not do:
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);
Upvotes: 3