【问题标题】:JPA/Hibernate with Spring Transactions not commiting未提交 Spring 事务的 JPA/Hibernate
【发布时间】:2017-05-29 13:09:02
【问题描述】:

我最近将 JpaTransactionManager 添加到我的配置中并尝试使用 Spring 事务。它似乎工作正常,除了我在下面添加的一项测试。

为什么我没有收到预期的IllegalStateException?我预计这会失败,因为CascadeTypeparentCategory 字段上设置为NONE。 如果我在我的存储库类中添加em.flush();,我会得到预期的异常。

据我了解,在我的测试中添加@Transaction 只会添加开始、提交和回滚事务方法。所以它应该在提交方法上失败,但它没有。

根据Hibernate User Guidehibernate.transaction.flush_before_completion 默认为 false。这就是我没有得到预期异常的原因吗?

有一个similair post 用户声明提交将导致刷新。这也是我决定问这个问题的原因。


这是我的实体

@Entity
@Table(name = "category")
public class Category {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "category_id", nullable = false, insertable = false, updatable = false)
  private Long id;

  @Column(name = "category", nullable = false, unique = true)
  private String categoryName;

  @ManyToOne // Default is CascadeType.NONE
  @JoinColumn(name = "parent_category_id", referencedColumnName = "category_id")
  private Category parentCategory;

  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parentCategory")
  private List<Category> childCategories = new ArrayList<>();

  public Category(String categoryName, Category parentCategory) {
    this.categoryName = categoryName;
    this.parentCategory = parentCategory;
  }

  // default constructor, getters and setters ...
}

这是我的仓库

@Repository
public class CategoryDaoImpl {

  @PersistenceContext protected EntityManager em;

  @Override
  public E persist(E entity) {
    em.persist(entity);
    // em.flush(); // Why doesnt Spring transactions automatically add the em.flush()  inside the transaction?
    return entity;
  }

}

这是我的测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DbConfig.class)
@Transactional
public class CategoryDaoTest {

  @Autowired private CategoryDao categoryDao;

  @Test(expected = IllegalStateException.class)
  public void createTwoCategoriesPersistChildAndGetIllegalStateException() {
    Category childCategory = new Category("firstChild", new Category("rootCategory", null));

    categoryDao.persist(childCategory);
  }

}

这是我的配置

@Configuration
@ComponentScan(basePackages = "nl.yoshuan.pricecomparer")
@EnableTransactionManagement
public class TestConfig {

  @Bean
  public DataSource dataSource() {
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    return builder.setType(EmbeddedDatabaseType.HSQL).build();
  }

  @Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("nl.yoshuan.pricecomparer.entities");
    factory.setDataSource(dataSource());

    Map<String, Object> jpaProperties = new HashMap<>();
    jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
    jpaProperties.put("hibernate.show_sql", true);
    jpaProperties.put("hibernate.format_sql", true);
    jpaProperties.put("hibernate.use_sql_comments", true);
    jpaProperties.put("hibernate.hbm2ddl.auto", "create-drop");
    factory.setJpaPropertyMap(jpaProperties);

    return factory;
  }

  @Bean
  public PlatformTransactionManager transactionManager() {
    JpaTransactionManager txManager = new JpaTransactionManager();
  txManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return txManager;
  }

}

这是我的控制台输出

2017-05-29 15:43:14 INFO  TransactionContext:101 - Began transaction (1) for test context [DefaultTestContext@971e903 testClass = CategoryDaoUTest, testInstance = nl.yoshuan.pricecomparer.dao.CategoryDaoUTest@2b2f5fcf, testMethod = addTwoCategoriesPersistChildAndGetIllegalStateException@CategoryDaoUTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@108531c2 testClass = CategoryDaoUTest, locations = '{}', classes = '{class nl.yoshuan.pricecomparer.config.TestConfig}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@7661b5a]; rollback [true]
Hibernate: 
    /* insert nl.yoshuan.pricecomparer.entities.Category
        */ insert 
        into
            category
            (category_id, category, parent_category_id) 
        values
            (default, ?, ?)
2017-05-29 15:43:14 INFO  TransactionContext:136 - Rolled back transaction for test context [DefaultTestContext@971e903 testClass = CategoryDaoUTest, testInstance = nl.yoshuan.pricecomparer.dao.CategoryDaoUTest@2b2f5fcf, testMethod = addTwoCategoriesPersistChildAndGetIllegalStateException@CategoryDaoUTest, testException = java.lang.AssertionError: Expected exception: java.lang.IllegalStateException, mergedContextConfiguration = [MergedContextConfiguration@108531c2 testClass = CategoryDaoUTest, locations = '{}', classes = '{class nl.yoshuan.pricecomparer.config.TestConfig}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]].
2017-05-29 15:43:14 INFO  GenericApplicationContext:987 - Closing org.springframework.context.support.GenericApplicationContext@49206065: startup date [Mon May 29 15:43:14 CEST 2017]; root of context hierarchy
2017-05-29 15:43:14 INFO  LocalContainerEntityManagerFactoryBean:548 - Closing JPA EntityManagerFactory for persistence unit 'default'
2017-05-29 15:43:14 INFO  SchemaDropperImpl$DelayedDropActionImpl:524 - HHH000477: Starting delayed drop of schema as part of SessionFactory shut-down'
Hibernate: 

    alter table category 
       drop constraint FKs2ride9gvilxy2tcuv7witnxc
Hibernate: 

    drop table category if exists
2017-05-29 15:43:14 INFO  EmbeddedDatabaseFactory:217 - Shutting down embedded database: url='jdbc:hsqldb:mem:testdb'

java.lang.AssertionError: Expected exception: java.lang.IllegalStateException

    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

【问题讨论】:

  • 更新了控制台输出。

标签: java spring hibernate spring-transactions


【解决方案1】:

我认为您应该将@TransactionConfiguration 添加到您的单元测试中。 TransactionConfiguration 定义用于配置事务测试的类级元数据。

【讨论】:

  • @TransactionConfiguration 已弃用,不是我想要的。 @Rollback 也不是。 @Commit 确实有效,但现在异常发生在我的方法之后。我得到java.lang.AssertionError: Expected exception: java.lang.IllegalStateException 之后,我得到了我的 InvalidDataAccessApiUsageException --> TransientPropertyValueException --> IllegalStateException stacktrace
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-13
  • 2015-12-17
  • 1970-01-01
  • 2017-06-17
  • 1970-01-01
  • 2016-10-23
相关资源
最近更新 更多