日期:2014-05-20 浏览次数:20765 次
2009-6-18 23:41:46 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet default threw exception java.lang.NoClassDefFoundError: org/hibernate/Session at com.starc.action.sort.SortAction.execute(SortAction.java:38) 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 com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404) at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229) at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept
package com.starc.action.sort; import java.util.Set; import com.opensymphony.xwork2.ActionSupport; import com.starc.dao.ISortDao; import com.starc.dao.impl.SortDaoImpl; import com.starc.hibernate.Sort; /** * * @author starc * * the action will be init * */ public class SortAction extends ActionSupport { private Sort sort; private Set<Sort> childs; public Set<Sort> getChilds() { return childs; } public void setChilds(Sort parent) { this.childs = parent.getChildSet(); } public Sort getSort() { return sort; } public void setSort(Sort sort) { this.sort = sort; } @Override public String execute() throws Exception { ISortDao sd=new SortDaoImpl(); sd.findById(1); return super.execute(); } }
package com.starc.dao.impl; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.starc.dao.HibernateUtils; import com.starc.dao.ISortDao; import com.starc.hibernate.Sort; public class SortDaoImpl implements ISortDao { public boolean addSort(Sort child, Sort parent) { boolean state = false; if (this.check(parent) || !this.check(child) || !parent.isLeaf()) { child.setParent(parent); Session session = HibernateUtils.getSession(); Transaction trans = session.beginTransaction(); try { session.save(child); trans.commit(); state = true; } catch (Exception e) { e.printStackTrace(); trans.rollback(); } finally { if (session.isOpen()) { HibernateUtils.closeSession(session); } } } return state; } public void delete(Sort sort) { if (this.check(sort)) { Session session = HibernateUtils.getSession(); Transaction trans = session.beginTransaction(); try { session.delete(sort); trans.commit(); } catch (Exception e) { e.printStackTrace(); trans.rollback(); } finally { if (session.isOpen()) { HibernateUtils.closeSession(session); } } } } public Sort findById(int id) { Sort sort = null; System.out.println(id); //到这都正常,就是session这,用测试类也正常 Session session = HibernateUtils.getSession(); Transaction trans = session.beginTransaction(); try { sort = (Sort) session.get(Sort.class, id); trans.commit(); } catch (Exception e) { e.printStackTrace(); trans.rollback(); } finally { HibernateUtils.closeSession(session); } return sort; } @SuppressWarnings("unchecked") public Sort findByName(String name) { Sort sort = null; Session session = HibernateUtils.getSession(); Transaction trans = session.beginTransaction(); try { List list = session.createQuery( "from Sort as sort where sort.name=" + name).list(); if (!list.isEmpty()) { sort = (Sort) list.get(0); } trans.commit(); } catch (Exception e) { e.printStackTrace(); trans.rollback(); } finally { if (session.isOpen()) { HibernateUtils.closeSession(session); } } return sort; } @SuppressWarnings("unchecked") public List<Sort> getChilds(Sort sort) { List<Sort> list = new ArrayList(); Session session = HibernateUtils.getSession(); Transaction trans = session.beginTransaction(); try { List tmp = session.createQuery( "from Sort as sort where sort.parent=" + sort.getId()) .list(); trans.commit(); Iterator iterator = tmp.iterator(); while (iterator.hasNext()) { Sort tmpSort = (Sort) iterator.next(); list.add(tmpSort); } } catch (Exception e) { e.printStackTrace(); trans.rollback(); } finally { if (session.isOpen()) { HibernateUtils.closeSession(session); } } return list; } public Sort getParent(Sort sort) { Sort parent = null; sort.getParent(); return parent; } public LinkedList<Sort> getPath(Sort sort) { LinkedList<Sort> path = new LinkedList<Sort>(); while (sort.getParent() != null) { path.addFirst(sort.getParent()); sort = sort.getParent(); } return path; } public Sort modify(Sort sort) { Session session = HibernateUtils.getSession(); Transaction trans = session.beginTransaction(); try { if (this.findById(sort.getId()) != null) { session.update(sort); trans.commit(); } } catch (Exception e) { e.printStackTrace(); trans.rollback(); } finally { if (session.isOpen()) { HibernateUtils.closeSession(session); } } return sort; } public boolean check(Sort sort) { boolean state = false; Session session = HibernateUtils.getSession(); Transaction trans = session.beginTransaction(); try { Sort tmpSort = (Sort) session.get(Sort.class, sort.getId()); if (sort.getName() == tmpSort.getName() || sort.getParent().getId() == tmpSort.getParent().getId()) { state = true; } } catch (Exception e) { e.printStackTrace(); trans.rollback(); } finally { if (session.isOpen()) { HibernateUtils.closeSession(session); } } return state; } public Sort getRoot() { Sort root = null; root = this.findById(1); return root; } }