spring-boot - Spring JPA 投影 - 在嵌套对象列表中选择特定列
问题描述
为什么我收到以下错误:
查询指定连接提取,但提取关联的所有者不在选择列表中
当我尝试获取 ID 列表时?
@Query("SELECT at.id FROM Template at " +
"WHERE at.shipper.id = :companyId " +
"AND at.isActive = true")
@EntityGraph(attributePaths = {"shipper"})
List<Long> findTemplateIdsByCompanyId2(Long companyId, Pageable pageable);
但是当我想获取对象列表时 - 一切都好吗?
@Query("SELECT at FROM Template at " +
"WHERE at.shipper.id = :companyId " +
"AND at.isActive = true")
@EntityGraph(attributePaths = {"shipper"})
List<Template > findTemplateIdsByCompanyId2(Long companyId, Pageable pageable);
Template
实体与字段有关系OneToOne
和与字段有关系shipper
OneToMany
warehouse
解决方案
如果您不获取实体,则需要加入查询,应该这样做:
SELECT at.id FROM Template at JOIN at.Shipper s WHERE s.id = :companyId and at.isActive = true
推荐阅读
- python - 在 Python 中创建 3D 数组
- python - 如何在 django 视图中获取隐藏输入的值
- java - 在java的整个程序中使用一个sql连接对象
- vue.js - Laravel 根据路线更改布尔值
- inno-setup - 避免重复安装两次的相同安装程序
- c++ - 检查输入 std::array 指针数据是否等于某个常量数组
- sap-fiori - 在 Fiori 中将 smartchart 与 smarttable 连接起来
- powershell - 转换大小写 - 将特定 csv 列中的大写转换为小写
- php - magento 2如何在产品详细信息选项卡中获取产品对象
- php - Laravel API 端点不适用于服务器别名