mysql - Junit 在使用 session.save 但不在 MySQL 数据库表中插入数据时成功,尽管我也尝试刷新 session

标签 mysql hibernate jpa orm junit

带有 applicationContext.xml 的简单 DAO 层请在我的 applicationContect.xml 下面找到 请帮助我理解为什么我的 Junit 说成功但数据没有插入数据库。

<?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:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
   xsi:schemaLocation="  
http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.0.xsd  
http://www.springframework.org/schema/tx  
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.VideoRental.*" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>com.VideoRental.domain.Category</value>
<value>com.VideoRental.domain.Customer</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.hbm2ddl.import_files">import.sql</prop>  
<prop key="connection.autocommit">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">true</prop> 

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

</beans>

最佳答案

默认情况下,@Transactional 测试将在测试方法完成时回滚所有更改。所以即使测试通过,修改也只对当前运行的Transaction有效。要保留您的更改,您需要提交事务。

查看 this SO question 上的示例.

您可以将 @Transactional 替换为 transactionTemplate 以在完成测试之前提交事务。

关于mysql - Junit 在使用 session.save 但不在 MySQL 数据库表中插入数据时成功,尽管我也尝试刷新 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23867224/

相关文章:

java - H2 未在我的 Spring Boot 应用程序中创建/更新表。我的实体有问题吗?

php - MYSQL具体搜索

php pdo 使用变量更新查询

java - Hibernate/Spring HibernateTemplate.findByCriteria(Deatched Criteria dc) 在 View 上执行sql更新

hibernate - 我们可以在 Hibernate HQL 查询中连接两个属性吗?

java - 使用外键引用反序列化 JPA 实体或如何为映射实体设置外键 ID

php mysql select insert 在同一台服务器的不同数据库上

php - Magento 交易电子邮件不工作

java - 传递分离的实体以使用额外的列来持久保存多对多

java - JPA OneToMany 和 ManyToOne 抛出 : Repeated column in mapping for entity column (should be mapped with insert ="false" update ="false")