java - 如何针对实体图发出一个查询而不是多个查询
问题描述
我有一个 Spring Data 存储库,我在其中获取一组帐户的所有职位:
public interface PositionRepository extends JpaRepository<Position, PositionKey> {
@EntityGraph(value = "Position.all", type = EntityGraphType.LOAD)
List<Position> findByAccountIn(Set<Account> accounts);
}
Position
具有也是实体的属性(一些嵌套级别):
@Entity
@NamedEntityGraph(name = "Position.all",
attributeNodes = {@NamedAttributeNode("account", subgraph = "Account.all"),
@NamedAttributeNode("product", subgraph = "Product.all")
})
@Data
public class Position {
@EmbeddedId
private PositionKey positionKey;
@MapsId("accountId")
@ManyToOne
@JoinColumn(name = "accountId")
private Account account;
@MapsId("productId")
@ManyToOne
@JoinColumn(name = "productId")
private Product product;
}
@Embeddable
@Data
public class PositionKey implements Serializable {
@Column(name = "accountId")
private Long accountId;
@Column(name = "productId")
private Long productId;
}
@Entity
@NamedEntityGraph(includeAllAttributes = true, name = "Product.all",
attributeNodes = {@NamedAttributeNode(value = "vendor", subgraph = "Vendor.all"),
@NamedAttributeNode(value = "type", subgraph = "ProductType.all")}
)
@Data
public class Product {
@Id
@Column(name = "id")
private Long id;
@ManyToOne
@JoinColumn(name = "typeId")
private ProductType type;
@ManyToOne
@JoinColumn(name = "vendorCode")
private Vendor vendor;
}
@Entity
@NamedEntityGraph(name = "Account.all")
@Data
public class Account {
@Id
@Column(name = "accountId")
private Long id;
}
我总是将位置的完整实体图作为序列化的 JSON 返回给客户端。所以,我总是需要所有属性和嵌套属性。位置实例最多为 350 个。
尽管使用@NamedEntityGraph
了 ,但我注意到仍在发出个别查询。例如,我获取每个唯一供应商的查询。从所有单个查询中获取结果通常需要大约 2-3 秒。
如何告诉 JPA 发出一个带有多个连接的查询?我可以手动编写这个 SQL(使用多个连接),它会在几毫秒内返回。
更新
数据访问如下:
@RestController
@RequestMapping("/position")
@RequiredArgsConstructor
public class PositionController {
private final PositionRepository positionRepo;
@GetMapping
public List<Position> getAllPositions(Set<Account> accounts) {
return positionRepo.findByAccountIn(account);
}
}
当位置列表返回时,它们通过 Jackson 序列化。因此,实际访问数据的是杰克逊。
解决方案
推荐阅读
- ghostscript - pdfgetpage MediaBox 混合了宽度和高度
- javascript - 使用 Javascript 在鼠标悬停时显示 div 或在鼠标悬停时将 div 隐藏到 forEach 中的功能
- ios - 在本地查找 dSYM 文件
- node.js - 在节点中生成、存储/恢复公钥/私钥
- primefaces - 更新标签中断面板正在更新
- python - 使用 Beautiful Soup 抓取亚马逊评论
- python - Python pandas:如果A列值出现不止一次,则分配B列的第一个值
- javascript - javascript - 具有键/值对的数组
- php - PDF 多数字标牌
- python - 如何将所有数据框行的某些列附加到另一个