HTTP状态500 - 如果没有活动事务,createCriteria无效

时间:2013-02-27 12:22:46

标签: hibernate hibernate-criteria

晚上好,请帮助我解决这些异常我正在使用hibernate 3.5从数据库生成DAO和实体

的hibernate.cfg.xml

<hibernate-configuration>
<session-factory name="SessionFactory">
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/automobiles</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.search.autoregister_listeners">false</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <mapping resource="entity/User.hbm.xml" />
    <mapping resource="entity/Model.hbm.xml" />
    <mapping resource="entity/Manufucturer.hbm.xml" />
    <mapping resource="entity/UserOrderCar.hbm.xml" />
    <mapping resource="entity/CarPhotos.hbm.xml" />
    <mapping resource="entity/Car.hbm.xml" />
</session-factory>

DaoClass

public List extractAdminAttributes() {
    log.debug("extractAdminAttributes");
    try {
        Session session=sessionFactory.getCurrentSession();
        List productList = sessionFactory
                .getCurrentSession()
                .createCriteria("entity.Car")
                .setProjection(
                        Projections.projectionList()
                                .add(Projections.property("carDesc"))
                                .add(Projections.property("quantity"))
                                .add(Projections.property("price"))).list();
        return productList;

    } catch (RuntimeException re) {
        log.error("extractAdminAttributes failed", re);
        throw re;

    }

}

这是异常的堆栈跟踪:

org.hibernate.HibernateException: createCriteria is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:340)
at $Proxy6.createCriteria(Unknown Source)
at entity.CarHome.extractAdminAttributes(CarHome.java:138)
at servlets.AdminServlet2.populateAdminView(AdminServlet2.java:65)
at servlets.AdminServlet2.doGet(AdminServlet2.java:48)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

2 个答案:

答案 0 :(得分:1)

Spring为我们管理交易,所以请从弹簧配置中删除以下行

<property name="hibernate.current_session_context_class">thread</property>

那应该让它发挥作用

答案 1 :(得分:0)

如果您还没有查询,则需要在执行查询之前启动事务。试试这样的事情:

public List extractAdminAttributes() {
    log.debug("extractAdminAttributes");
    try {
        Session session = sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();
        List productList = session
                .createCriteria("entity.Car")
                .setProjection(
                        Projections.projectionList()
                                .add(Projections.property("carDesc"))
                                .add(Projections.property("quantity"))
                                .add(Projections.property("price"))).list();

        tx.commit();  
        return productList;

    } catch (RuntimeException re) {
        tx.rollback();
        log.error("extractAdminAttributes failed", re);
        throw re;
    }
}