当前位置:网站首页>JPA循环保存多个实体失败
JPA循环保存多个实体失败
2022-06-11 16:58:00 【wozaizhe.55】
用JPA循环保存多个实体,代码如下:
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);
}
}理想情况是, 外层循环执行一次,内层循环保存a.getTopic().size()个实体. 然而事实并非如此, 查看数据库便可发现,仅仅是a.getTopics()数组中最后一个元素(topic)对应的实体得到了保存, 难道其他元素(topic)对应的实体都没有得到保存吗?
事实上, 不是的, 前边topic对应的实体也得到了保存,只是被后边的topic对应的实体给覆盖了.为什么会这样呢? 因为test是在外层循环实例化的, 对于内层循环来说, test对象是相同的, 因此执行save()操作时, JPA认为后边topic是对前边topic的更新. 所以外层循环执行一次, 最终至多有一个实体得到保存.
若想让每个topic 对应的实体都能够得到保存, 应该将test的实例化操作放在内层循环,如下:
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);
}
}这样每次执行save()操作时,test对象都是不同的,因而可以完成对每个topic的保存.
边栏推荐
- How unittest knows the execution time of each test case
- WYSIWYG markdown editor: typora
- 微信小程序时间戳转化时间格式+时间相减
- Global and China Mobile Network Optimization (MnO) industry development panoramic survey and Investment Strategy Research Report 2022-2028
- RSP:遥感预训练的实证研究
- Opencv相机标定之圆形标识点中心检测
- How to disable the notebook's own keyboard after the notebook is connected to an external keyboard
- unittest 如何知道每个测试用例的执行时间
- 美团获得小样本学习榜单FewCLUE第一!Prompt Learning+自训练实战
- ASP. Net education OA system source code education industry OA system source code with document
猜你喜欢

Solve the problem that jupyter cannot connect to the kernel based on pycharm and Anaconda -- solution 1

核密度估计(二维、三维)

Real time myth -- real-time RTOS multitask performance analysis

Analysis report on sales status and supply and demand prospects of phosphoric acid fuel cell industry in the world and China 2022-2028 Edition

Using MATLAB and dcraw to process digital camera raw files

Elasitcsearch basic learning notes (1)

Research Report on operation mode and investment opportunities of China's aluminum industry 2022-2028

一套ThinkPHP微信小程序商城源码带后台管理

Regression prediction | realization of RBF RBF neural network with multiple inputs and single output by MATLAB

LeetCode——42. Connected to rainwater (double pointer)
随机推荐
2022 R1 quick opening pressure vessel operation test question bank and simulation test
What products are good for cross-border e-commerce? What are the top selling categories?
【opencvsharp】opencvsharp_ samples. Core sample code Notes
做跨境电商卖什么产品好?热销类目有哪些?
每周推荐短视频:菜鸟CEO谈未来物流新赛道
Weekly recommended short video: rookie CEO talks about the new logistics track in the future
接口的特点&&接口与抽象类的对比
tornado环境搭建及基本框架搭建——熟悉的hello world
golang中的异常处理和异常使用
Message queue push / pull mode Learning & ActiveMQ and JMS learning
Center detection of circular marking points for opencv camera calibration
Elasitcsearch基础学习笔记(1)
2022年高处安装、维护、拆除考试模拟100题及在线模拟考试
Katalon Studio Enterprise
RSP: An Empirical Study of remote sensing pre training
Char array parsing
Using MATLAB and dcraw to process digital camera raw files
Connection and difference of network streaming media protocol (RTP RTCP RTMP HLS)
C语言:使用.h和.c文件遇到的问题总结
Learning C language from scratch day 039