Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选择不存在实体的默认JPA行为_Jpa - Fatal编程技术网

选择不存在实体的默认JPA行为

选择不存在实体的默认JPA行为,jpa,Jpa,我正在编写一些测试,以确保所有CRUD方法都正常工作。它们中的每一个都工作得很好,但是测试remove方法似乎有点棘手 在我的测试中,我这样做: // remove a = dao.select(1); // previously inserted in the DB dao.remove(a); assertNull(dao.select(a.getId())); 和DAO类(仅选择并删除): 但是测试总是在remove之后的行上抛出javax.persistence.EntityNotFo

我正在编写一些测试,以确保所有CRUD方法都正常工作。它们中的每一个都工作得很好,但是测试remove方法似乎有点棘手

在我的测试中,我这样做:

// remove
a = dao.select(1); // previously inserted in the DB
dao.remove(a);
assertNull(dao.select(a.getId()));
和DAO类(仅选择并删除):

但是测试总是在
remove
之后的行上抛出
javax.persistence.EntityNotFoundException
。这是正常行为还是真的出了问题?如果这看起来很明显,很抱歉,但我找不到答案。

getReference()
将永远不会返回null。阅读其文档:

获取一个实例,该实例的状态可能是延迟获取的。如果请求的实例在数据库中不存在,则在第一次访问实例状态时抛出EntityNotFoundException


此方法返回假定存在的实体上的代理。它甚至不进行数据库查询来检查实体是否存在:它假设实体存在。如果代理稍后初始化,并且实体不存在,那么您将得到一个EntityNotFoundException。

谢谢,我刚刚在中找到了相同的东西。我用
find
替换了它,现在它可以正常工作了。
@Override
 public AtividadeComercial select(int id) {
      return em.getReference(AtividadeComercial.class, id);
}

@Override
public void remove(AtividadeComercial e) {
    EntityTransaction t = em.getTransaction();
    boolean active = t.isActive();
    if(!active)
        t.begin();
    em.remove(em.getReference(e.getClass(), e.getId()));
    if(!active)
        t.commit();
}