如何使用CrudRepository将修改后的数据附加到数据库中,而不是替换SpringBoot

我使用CrudRepository对象为我的实体类获取对象列表。我想复制数据库中的列表,并为修改的列表中的每个Object复制一些字段。如何使用相同的存储库对象实现它?


示例代码如下。它修改了Database中的现有行。我希望它将修改作为新行追加到数据库中。


@Autowired TestRepository testRepository;


List<SampleClass> sampleList = testRepository.findAllBySomeId(id);

sampleList.forEach(i -> i.setSomeValue(1234));

testRepository.saveAll(testRepository);

我需要制作单独的Repository对象吗?是否可以使用@Autowired注释相同的Repository对象?


慕哥6287543
浏览 1315回答 2
2回答

回首忆惘然

autowire&nbsp;EntityManager,detach实体并将id设置为nullsampleList.forEach(i&nbsp;->&nbsp;{ &nbsp;&nbsp;&nbsp;entityManager.detach(i); &nbsp;&nbsp;&nbsp;i.setId(null); &nbsp;&nbsp;&nbsp;i.setSomeValue(1234);});然后使用相同的存储库保存列表
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java