0

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

休眠.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>

道类

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)
4

2 回答 2

1

Spring 为我们管理事务,所以请从 spring 配置中删除以下行

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

那应该使它起作用

于 2014-09-04T16:56:38.397 回答
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;
    }
}
于 2013-02-27T13:18:48.910 回答