database - 保存休眠对象而不获取外键实体,仅按 id 保存
问题描述
我正在将带有 Hibernate 的对象“产品”保存到数据库中。Product 对象具有不作为 id 的依赖对象。在数据库中,它被保存为外键。下面的代码是获取保存对象 Product 的依赖对象的一部分。所以每次保存产品时,我们都会对数据库进行不必要的调用,因为我们已经知道依赖对象的 id。我怎样才能以更好的方式做到这一点?
Rubric rubric = rubricRepository.findById(productRequestDto.getRubricId())
.orElseThrow(() -> new EntityNotFoundException(productRequestDto.getRubricId().toString()));
User user = userRepository.findById(productRequestDto.getUserId())
.orElseThrow(() -> new EntityNotFoundException(productRequestDto.getUserId().toString()));
Condition condition = conditionRepository.findById(productRequestDto.getConditionId())
.orElseThrow(() -> new EntityNotFoundException(productRequestDto.getConditionId().toString()));
Color color = colorRepository.findById(productRequestDto.getColorId())
.orElseThrow(() -> new EntityNotFoundException(productRequestDto.getColorId().toString()));
解决方案
推荐阅读
- javascript - 基于一定的时间间隔清除超时
- woocommerce - 如何在 Wordpress 中隐藏“WooCommerce 搜索结果中的缺货产品”?
- android - 找不到 SDK Manager 的 GUI
- arrays - 阵列无助于速度
- c - 我正在创建自己的 UNIX shell,有时当我运行“ls”命令时,它会给出错误错误地址
- c++ - 通过 Clion 的 CMake 使用 PDCurses 静态链接库的 C++ 64 位项目
- html - FlexBox:左角对齐图标并在它们之后保持相同的间隙
- javascript - 使用 Jquery 选择鼠标点击点的颜色十六进制代码
- android - 在配置的虚拟设备上运行应用程序
- jquery - jquery hover().addClass() 不响应请求