简体   繁体   English

将持久化实体保留在事务之外

[英]Keep persisted entities outside of transaction

I have a scenario where I am trying to import complex data to my database (objects with child objects and list objects, some for updates while others for new inserts).我有一个场景,我试图将复杂数据导入我的数据库(具有子对象和列表对象的对象,一些用于更新,而另一些用于新插入)。 Right now, the general process I'm working with is the following:现在,我正在使用的一般流程如下:

void importData(List dataToImport){
     Object persistedObject1 = getObjectFromDB(); // data that will be attached to various objects in my import
     HashMap map1 = preFillMap1();  // Fills a map with persisted objects from database to be attached to data
     HashMap map2 = emptyMap(); // start with an empty map here
     try{
         for(Object data : dataToImport){
                Object dbSavedData = importDataImpl(persistedObject1, map2, map3, data)
                map2.put(dbSavedData.getChild().getRecordID(), dbSavedData.getChild())
         }
     }catch(Exception e){
           logger.error("Error in transaction", e);
     }
}

@org.springframework.transaction.annotation.Transactional
void importDataImpl(Object persistedObj, HashMap map1, HashMap map2, Object dataToImport){
      // simplified import process
      dataToImport.setForeignKeyObject1(persistedObj);
      dataToImport.setForeignKeyObject2(map1.get(dataToImport.obj2ID));
      if(map2.contains(dataToImport.getChild().getRecordID()){
            dataToImport.setForeignKeyObject3(map2.get(dataToImport.obj3ID));
      }else{
            dataToImport.setForeignKeyObject3(repo.findByID(dataToImport.getChild().getRecordID());
      }
      jpaRepo.save(dataToImport);           
}

The problem I'm running into in the above is that once I hit an error in importDataImpl, I run into an error for future calls with the following:我在上面遇到的问题是,一旦我在 importDataImpl 中遇到错误,我会在以后的调用中遇到以下错误:

detached entity passed to persist: com.project.ChildObject; nested exception is org.hibernate.PersistentObjectException: 
detached entity passed to persist: com.project.ChildObject org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist:com.project.ChildObject

The only way so far I've been able to work around it is to reset the maps on error and retry the importDataImpl call.到目前为止,我能够解决它的唯一方法是在出错时重置地图并重试 importDataImpl 调用。 It works the second time around without an issue, but the goal of keeping the maps is to reduce the number of DB calls needed.它在第二次运行时没有问题,但保留地图的目的是减少所需的数据库调用次数。 I've seen some recommendations on doing an entityManager.contains(obj) check, but this isn't reliable from what I can tell nor is it the most elegant solution either as I would still have to reset the maps.我已经看到了一些关于进行 entityManager.contains(obj) 检查的建议,但据我所知,这并不可靠,也不是最优雅的解决方案,因为我仍然需要重置地图。 At that point, catching the error and handling it there seems more cleaner.在这一点上,捕捉错误并在那里处理它似乎更干净。

Perhaps you could try entity.merge().也许你可以试试 entity.merge()。

merge() will push the stale state to the DB, and overwrite any intervening updates merge() 会将过时的 state 推送到数据库,并覆盖任何干预更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM