Java 批更新-具有相同标识符值的不同对象已与会话关联

Java 批更新-具有相同标识符值的不同对象已与会话关联,java,jpa,spring-data-jpa,Java,Jpa,Spring Data Jpa,我搜索了一些答案,但找不到与我相似的案例 我有几个字段的实体类,它们都是简单的java变量类型。它们都不是多对一的 @Data @Entity @Builder @EqualsAndHashCode public class TPXXXXX @Id @SequenceGenerator(name = "STAGING_XXXXX", sequenceName = "STAGING_XXXXX_SEQ") @GeneratedValue(strategy

我搜索了一些答案,但找不到与我相似的案例

我有几个字段的实体类,它们都是简单的java变量类型。它们都不是多对一的

@Data
@Entity
@Builder
@EqualsAndHashCode
public class TPXXXXX

@Id
@SequenceGenerator(name = "STAGING_XXXXX", sequenceName = "STAGING_XXXXX_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "STAGING_XXXXX")
private Long id;
我在处理过程中所做的就是-

  • 从存储库中查找id和对象之间的映射
  • 迭代上面映射出来的列表
  • 创建一个新实体
  • 存储新实体
  • private int methodOne(List<String> ids){
    
        Map<String, List<Tpo>> idAndObjMappings = repository.findXXXXX(ids).stream().collect(Collectors.groupingBy(Tpo::getId));
    
        for(String id: ids){
    
            List<Tpo> tpoList = idAndObjMappings.get(id);
            List<TPXXXXX> tpxxxx = generateAnotherEntity(tpoList);
            tpxxxx.addAll(tpxxxx);
        }
        
        int count = store(tpxxxx);
        
        }
    
    private List<TPXXXXX> generateAnotherEntity(List<Tpo> tpoList){
        List<TPXXXXX> tpEntityList = new ArrayList<>();
        tpoList.forEach(tpo -> {
            TPXXXXX tpEntity = TPXXXXX.builder().settersU().build();
            tpEntityList.add(tpEntity)
            }
            return tpEntityList;
        }
        
        
        private int store(List<TPXXXXX> tpList){
            int entityCount = 0;
            for(TPXXXXX tp: tpList){
            
                if (entityCount > 0 && (entityCount % batchSize == 0 || entityCount == tpList.size())) {
                    entityManager.flush();
                    entityManager.clear();
                }
                entityManager.persist(tp);
                entityCount++;
            }
            return entityCount;
        }
    
    
    Caused by: org.springframework.dao.DataIntegrityViolationException: A different object with the same identifier value was already associated with the session : 
    [.TPXXXXXX#2693]; nested exception is javax.persistence.EntityExistsException: 
    A different object with the same identifier value was already associated with the session : [TPXXXXX#2693]