Spring 启动 INSERT 参数

Spring Boot INSERT parameters

我需要在我的 spring project.Code 部分中亲自更新 person_id class 因为 follows.But 插入查询在存储库部分不起作用.需要作为参数一个一个发送吗?

public void save(){
     List<Person> personList = service.findByAll();
            for (Person person : personList) {
                personRepository.getNextSequence(person);
            }
    }


@Repository
   public interface PersonRepository extends JpaRepository<Person, Integer> {
    
        @Modifying
        @Query(value = "INSERT INTO PERSON(person_id,name) VALUES(PERSON_ID_SEQ.NEXTVAL,:person.name) ", nativeQuery = true)
        public void getNextSequence( Person  person);
    
     }

为了能够使用新ID保存同一个对象,您必须分离实体,将ID设置为0并保存。这将使用新 ID 保存相同的对象。

所以代码应该是这样的:

// somewhere in the code
@Autowired
private EntityManager entityManager;    

List<Person> personList = service.findByAll();

for (Person person : personList) {
    entityManager.detach(person);
    person.setId(0);
}

personRepository.saveAll(personList);