JUnit - org.hibernate.SessionException:会话已关闭

时间:2015-05-20 12:10:14

标签: java spring hibernate junit

我遇到了Hibernate会话的常见错误:LeftViewController *sampleProtocol = [[LeftViewController alloc]init]; LeftViewController.delegate = self; 查询。

我找到了一个解决方案,让我的测试类扩展javax.persistence.TransactionRequiredException: Executing an update/delete,但我不明白为什么有必要。

我尝试在我的项目上设置junit(4.11版本)测试。

  • 我的测试类:

_

AbstractTransactionalJUnit4SpringContextTests

UserPreferenceDAO已实例化。 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:config/ioc/application-context-spring-test.xml" }) @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class }) @TransactionConfiguration(transactionManager = "myTxManager") @Transactional public class UserPreferenceDAOTest extends AbstractTransactionalJUnit4SpringContextTests { @Autowired private UserPreferenceDAO dao; @Test @Transactional public void test() { try { SQLQuery query = dao.createSQLQuery("SELECT 1 from dual"); query.uniqueResult(); } catch (Exception e) { e.printStackTrace(); fail(); } } } 行是query.uniqueResult();行。

  • application-context-spring.xml:

包含数据库访问配置:

throws the javax.persistence.TransactionRequiredException
  • persistence.xml

_

    <!-- To precise the persistence configuration name file -->
    <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
        <property name="persistenceXmlLocations">
            <list>
                <value>classpath*:META-INF/persistence-web.xml</value>
            </list>
        </property>
        <property name="defaultDataSource" ref="dataSource" />
    </bean>

    <!-- EntityManagerFactory definition : JPA one -->
    <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager" ref="persistenceUnitManager" />
        <property name="persistenceUnitName" value="myPersistenceUnit" />
    </bean>

    <!-- Transaction Manager definition : JPA one-->
    <bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEmf" />
    </bean>

    <!-- Enable the annotation usage (bean injection for instance) -->
    <context:annotation-config />

    <!-- Enable annotation usage for transaction -->
    <tx:annotation-driven transaction-manager="myTxManager" proxy-target-class="false"/>

    <!-- Enterprise layer's dependencies -->
    <!-- UserPreferenceDAO bean statement here -->
    <import resource="classpath:ioc/0-model-enterpriselayer-dependencies.xml"/>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@myurl:1522:TD11P15A" />
        <property name="username" value="user" />
        <property name="password" value="pwd" />
    </bean>

我发现的大多数教程都没有使用此<persistence> <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <!-- with Annotations --> <class>com.airfrance.padme.server.entity.UserPreference</class> <!-- etc... (all my entities) --> <properties> <property name="hibernate.show_sql" value="false" /> <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" /> </properties> </persistence-unit> </persistence> 课程。示例:http://www.concretepage.com/spring-4/spring-4-junit-4-annotation-example

我哪里错了?

由于

1 个答案:

答案 0 :(得分:1)

根据文档AbstractTransactionalJUnit4SpringContextTests包括以下测试执行监听器:

  • ServletTestExecutionListener
  • DependencyInjectionTestExecutionListener
  • DirtiesContextTestExecutionListener
  • TransactionalTestExecutionListener
  • SqlScriptsTestExecutionListener

如果您不想从AbstractTransactionalJUnit4SpringContextTests扩展,但仍然希望事务行为只是将TransactionalTestExecutionListener添加到您的侦听器:

@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class})

TransactionalTestExecutionListener的javadoc:

  

TestExecutionListener,为在其中执行测试提供支持   通过尊重Spring的@Transactional进行测试管理的交易   注释