当前位置:网站首页>JPA failed to save multiple entities circularly
JPA failed to save multiple entities circularly
2022-06-11 17:05:00 【wozaizhe. fifty-five】
use JPA Cycle through multiple entities , The code is as follows :
for(A a : list){
Test test = new Test();
test.setId(a.getId());
test.setName(a.getName());
for(int i = 0; i < a.getTopics().size(); i ++){
test.setTopic(a.getTopics().get(i));
ITestDAO.save(test);
}
}Ideally , The outer loop is executed once , The inner layer circulates a.getTopic().size() Entity . But that's not the case , Look at the database and you will find , just a.getTopics() The last element in the array (topic) The corresponding entity is saved , Are other elements (topic) Have the corresponding entities not been saved ?
in fact , No, it isn't , in front topic The corresponding entity is also saved , Just by the back topic The corresponding entity is overwritten . Why is this so ? because test Is instantiated in an outer loop , For the inner loop , test Objects are the same , So execute save() In operation , JPA Think back topic It's right up front topic Update . So the outer loop executes once , Eventually at most one entity is saved .
If you want everyone to topic The corresponding entities can be saved , Should be test The instantiation of is placed in the inner loop , as follows :
for(A a : list){
for(int i = 0; i < a.getTopics().size(); i ++){
Test test = new Test();
test.setId(a.getId());
test.setName(a.getName());
test.setTopic(a.getTopics().get(i));
ITestDAO.save(test);
}
}This is done every time save() In operation ,test Objects are different , Thus, it is possible to topic The preservation of the .
边栏推荐
- Analyze which should be tested in PMP and ACP with actual cases? Which is more useful?
- Database backup (MySQL)
- LeetCode-859. Intimate string
- Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice
- Jinte Net Foundation will participate in the global strategy conference of dimension chain through online live broadcast
- Solr (II) Solr adds core and dependent package path
- Using MATLAB and dcraw to process digital camera raw files
- LeetCode——42. 接雨水(双指针)
- 啟牛商學院給的證券賬戶是安全的嗎?開戶收費嗎
- ^32 execution context stack interview questions
猜你喜欢

【opencvsharp】斑点检测 条码解码 图像操作 图像旋转/翻转/缩放 透视变换 图像显示控件 demo笔记

2022 simulated examination question bank and simulated examination for crane driver (limited to bridge crane)

Analysis report on future development trend and investment suggestions of global and Chinese soybean protein industry 2022-2028

A journey of database full SQL analysis and audit system performance optimization

Using MATLAB and dcraw to process digital camera raw files

Center detection of circular marking points for opencv camera calibration

Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice

WYSIWYG markdown editor: typora

基于文本驱动用于创建和编辑图像(附源代码)

Leetcode 1974. Minimum time to type words using a special typewriter (yes, once)
随机推荐
Leetcode 1974. Minimum time to type words using a special typewriter (yes, once)
接口的特点&&接口与抽象类的对比
[opencvsharp] spot detection barcode decoding image operation image rotation / flip / Zoom perspective transformation image display control demo notes
Global and Chinese molten carbonate fuel cell industry outlook and market panoramic Research Report 2022-2028
每周推荐短视频:菜鸟CEO谈未来物流新赛道
基于udp端口猜测的内网穿透
Is the securities account given by qiniu business school safe? Do you charge for opening an account
启牛推荐开通的股票账户安全吗?靠谱吗
leetcode--数组
7个人生工具:SWOT、PDCA、6W2H、SMART、WBS、时间管理、二八原则
LeetCode——42. Connected to rainwater (double pointer)
Analyze which should be tested in PMP and ACP with actual cases? Which is more useful?
2022 high altitude installation, maintenance and demolition test simulation 100 questions and online simulation test
消息队列-推/拉模式学习 & ActiveMQ及JMS学习
Science popularization genius on the left, madman on the right
Le compte de titres de l'école de commerce kainiu est - il sécurisé? Frais d'ouverture de compte
Is the stock account recommended by qiniu safe? Is it reliable
Chip mass production, oppo entering a new era?
Create database instance
solr(二)solr添加core以及依赖包路径