加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java-具有休眠状态的JUnit:此实例尚未在数据库中作为一行存在

发布时间:2020-12-15 01:20:29 所属栏目:大数据 来源:网络整理
导读:我正在使用JUnit进行测试. @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"classpath:com/mt/sm/application-context.xml","classpath:com/mt/sm/security-context.xml"})@TransactionConfiguration(transactionManager = "tr

我正在使用JUnit进行测试.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:com/mt/sm/application-context.xml","classpath:com/mt/sm/security-context.xml"})
@TransactionConfiguration(transactionManager = "transactionManager",defaultRollback = true)
@Transactional
public class LocationPathServiceImplTest { /* Class code here */ }

测试方法的声明非常简单:

@Test
public void testRefresh() { /* Method body */}

我已经创建了一个在setup()中保存obj的文件,然后保存到数据库.
在@Test中,我从DAO运行refresh()(refresh()方法仅调用EntityManager .refresh()),但这会导致以下错误

org.hibernate.HibernateException: this instance does not yet exist as a row in the database
javax.persistence.PersistenceException: org.hibernate.HibernateException: this instance does not yet exist as a row in the database

我不知道如何解决它.有人遇到过吗?
所有建议将不胜感激.

我决不会将更改提交到数据库,也不会调用.flush().据我所知,它们在当前事务中.

最佳答案
如果没有更多代码,您需要刷新DAO,以使实例持久化.刷新仅是对象级别,而刷新则是在数据库级别执行实际事务(因此rollback = true,因此在测试后会回滚)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读