Java Hades、Hibernate和JUnit:事务后不发生持久性
我有一个JUnit测试,需要先更新现有行的一列,然后再根据该列执行和验证进一步的操作。但我的问题是,即使在离开@Transactional边界之后,列也不会得到更新。我的代码如下所示Java Hades、Hibernate和JUnit:事务后不发生持久性,java,hibernate,jpa,junit,persistence,Java,Hibernate,Jpa,Junit,Persistence,我有一个JUnit测试,需要先更新现有行的一列,然后再根据该列执行和验证进一步的操作。但我的问题是,即使在离开@Transactional边界之后,列也不会得到更新。我的代码如下所示 @RunWith(UnitilsJUnit4TestClassRunner.class) @SpringApplicationContext( { "applicationContext.xml" }) public class TestClass { @SpringBeanByType TestTab
@RunWith(UnitilsJUnit4TestClassRunner.class)
@SpringApplicationContext(
{ "applicationContext.xml" })
public class TestClass
{
@SpringBeanByType
TestTableDBUtil util;
@Test
public void test()
{
util.updateColumnA(id, true);
/* I do not see the column A value commited on returning from this function */
/* Operations and validations based on the updated column ensue */
}
}
@Component
@ContextConfiguration(locations =
{ "/applicationContext.xml" })
@TransactionConfiguration
public TestTableDBUtil
{
@Autowired
private MyTableRepository myTableRepo;
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public void updateColumnA(Long id, boolean value)
{
MyEntity myEntity = myTableRepo.readByPrimaryKey(id);
myEntity.setColumnA(value);
myTableRepo.saveAndFlush(myEntity);
}
}
您是否在MyTableRepository的saveAndFlush()方法中进行合并?我决定使用Unitils事务,并在测试中将自动提交设置为true。无法使我的测试与SpringJUnit4TestClassRunner一起工作。看看您的bean配置文件如何?