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
Java 理解:“;无法对分离的对象执行删除操作;_Java_Jpa_Entity_Openjpa - Fatal编程技术网

Java 理解:“;无法对分离的对象执行删除操作;

Java 理解:“;无法对分离的对象执行删除操作;,java,jpa,entity,openjpa,Java,Jpa,Entity,Openjpa,删除时引发以下实体并出错: 实体: @Entity @NamedQueries({ }) public class Server { //some attributes } 我有以下非常简单的JUnit测试,它执行以下操作: 实体服务器已创建 @Transactional public class ServerDao { public Server update(Server entity, long userId) { entity.setDeleted(false);

删除时引发以下实体并出错:

实体:

@Entity
@NamedQueries({
})
public class Server {
   //some attributes
}
我有以下非常简单的JUnit测试,它执行以下操作:

  • 实体服务器已创建 @Transactional public class ServerDao { public Server update(Server entity, long userId) { entity.setDeleted(false); if (entity.getId() > 0) { if (userId > 0) { entity.setUpdated(new Date()); entity.setUpdatedby(usersDao.get(userId)); } em.merge(entity); } else { if (userId > 0) { entity.setInserted(new Date()); entity.setInsertedby(usersDao.get(userId)); } em.persist(entity); } return entity; } } 一切“似乎”都按预期工作,没有引发异常,记录从数据库中删除


    但是我不确定这意味着什么,我们真的需要在删除每个实体之前刷新它吗?因为我以前使用过EntityManager.delete多次,而不需要刷新实体。

    假设您使用的是Spring的
    @Transactional
    注释,它无法拦截来自注释对象实例内的调用(因为Spring对AOP和方法拦截使用了动态代理)。尝试从服务层调用并在那里注释方法


    如果您没有使用Spring,那么您可能希望使用
    @TransactionAttribute
    ,这是一个分析性的Java EE注释。

    您使用的是Spring@Transactional注释吗?您是对的,我们在Dao中使用了@Transactional。但我不明白它不能截获来自注释对象实例的调用。什么是“注释对象”?刀?我不是从dao中调用“deleteHardJUnit”,JUnit测试运行并首先调用update,然后在几行之后调用“deleteHardJUnit”。什么会阻止任何拦截器正确操作?解决方案是什么?编写JPQL“按ID删除”语句?或者在每次删除之前调用EntityManager.merge(entity)是另一种选择。 public void deleteHardJUnit(Server entity) { em.remove(entity); }
    public void deleteHardJUnit(Server entity) {
        if (entity.getId() > 0) {
            em.refresh(entity);
            em.remove(entity);
        }
    }