Org.hibernate.HibernateException: createQuery is not valid without active transaction

此生再无相见时 提交于 2019-12-06 09:29:42

Firstly, you should remove your hibernate.cfg.xml. By using <property name="packagesToScan" value="com.myproject.model" /> you are telling Hibernate to scan your Java classes for JPA annotations to build the database model with.

As a side effect, this will also remove the line <property name="dialect">org.hibernate.dialect.MySQLDialect</property> which needs to come out because Hibernate can select it's own dialect, and if you force a dialect it can cause all kinds of weird errors. I've never had a problem with Hibernate selecting a dialect for me.

If you ever get any kind of exception that says Connection cannot be null when 'hibernate.dialect' is not set, that means that Hibernate couldn't actually connect to your database.

Hibernate session context management is something that you won't need with Spring, as Spring is controlling the session context.

Your exception about the cannot find antlr and java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener as we discussed in chat, is because you were not using a dependency manager to pull in the classes that Spring/Hibernate were depending on.

You are certainly on the right track, using Spring to manage your sessions. The issue with the session not being open was due to the transactionManager and sessionFactory being mis-configured. I suggested you replace your pr-servlet with the following:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <tx:annotation-driven />
    <context:component-scan base-package="com.project"/>
    <mvc:annotation-driven/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:8889/MyProject" />
        <property name="username" value="jack" />
        <property name="password" value="jack" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.project.model" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
            </props>
        </property>
    </bean>

    <bean
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"
        id="PersistenceExceptionTranslator" />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

which got your transaction manager working.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!