之前使用测试还好好的 今天一试莫名出现问题!
package org.whatisjava.dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.whatisjava.domain.Category;
import org.whatisjava.util.HibernateSessionFactory;
public class CategoryDaoImpl implements CategoryDao {
public Category getCategoryById(Integer id) {
Session s = HibernateSessionFactory.getSession();
Category c = null;
try {
s.beginTransaction();
c = (Category) s.get(Category.class, id);
s.getTransaction().commit();
} catch (HibernateException e) {
s.getTransaction().rollback();
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return c;
}
public Category getCategoryByIdFetch(Integer id) {
return null;
}
public Category findById() {
return null;
}
}
结果报异常:
org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:149)
at org.whatisjava.dao.CategoryDaoImpl.getCategoryById(CategoryDaoImpl.java:18)
at org.whatisjava.controller.MyServletContext.contextInitialized(MyServletContext.java:22)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4336)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:761)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:741)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1023)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
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.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
package org.whatisjava.controller;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.whatisjava.dao.CategoryDao;
import org.whatisjava.dao.DaoFactory;
import org.whatisjava.domain.Category;
public class MyServletContext implements ServletContextListener
,ServletContextAttributeListener{
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("系统关闭....");
}
public void contextInitialized(ServletContextEvent context) {
System.out.println("系统启动....");