首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

关于hibernateCallBack()解决思路

2012-04-04 
关于hibernateCallBack()Java codepublic List search(final PetInfo condition, final String orderBy) {

关于hibernateCallBack()

Java code
public List search(final PetInfo condition, final String orderBy) {                                return super.getHibernateTemplate().executeFind(                new HibernateCallback() {                    public Object doInHibernate(Session s)                            throws HibernateException, SQLException {                        Criteria c = s.createCriteria(PetInfo.class);                        if (condition != null) {                            if (condition.getPetOwnerName() != null                                    && !"".equals(condition.getPetOwnerName()                                            .trim())) {                                c.add(Restrictions.like("petOwnerName",                                        condition.getPetOwnerName(),                                        MatchMode.ANYWHERE));                            }                            if (condition.getPetType() != null                                    && Integer.parseInt(condition.getPetType()                                            .toString()) > 0) {                                c.add(Restrictions.eq("petType", condition                                        .getPetType()));                            }                        }                        String order = orderBy;                        order = (null == orderBy || orderBy.trim().equals("")) ? "petName"                                : orderBy;                        c.addOrder(Order.asc(order));                        List ret = c.list();                        return ret;                    }                });    }



Java code
java.lang.NullPointerException    at com.aptech.jb.epet.dao.hibimpl.PetInfoDaoHibImpl.search(PetInfoDaoHibImpl.java:84)    at com.aptech.jb.epet.struts.action.PetAction.doList(PetAction.java:299)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)    at java.lang.reflect.Method.invoke(Method.java:597)    at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)    at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)    at com.aptech.jb.epet.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:30)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)    at java.lang.Thread.run(Thread.java:619) 




用hibernateCallBack的时候报空指针异常


return super.getHibernateTemplate().executeFind(
new HibernateCallback() {
这句报错

[解决办法]
HibernateCallback()是一个回调方法,在hibernate框架下,对数据库的一种操作,其实它使用很简单的,用这个方法是为了方便,为了创建session,须要在回调方法里比如:
public List selectList(final int startPage, final int endPage) {
List list = new ArrayList();
final String hql = "from Item order by id desc";
list = this.getHibernateTemplate().executeFind(new HibernateCallback() {

public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);//创建SESSION
query.setFirstResult(startPage);//分页设置
query.setMaxResults(endPage);
List list = query.list();获取结果结晶
return list;//返回值
}

});
return list;
 }

热点排行