spring - 如何将@NamedEntityGraph 与@EmbeddedId 一起使用?
问题描述
我正在尝试让 Spring Data JPA 使用连接发出一个查询以急切地获取实体图:
@Entity
@NamedEntityGraph(name = "PositionKey.all",
attributeNodes = {@NamedAttributeNode("positionKey.account"),
@NamedAttributeNode("positionKey.product")
})
@Data
public class Position {
@EmbeddedId
private PositionKey positionKey;
}
@Embeddable
@Data
public class PositionKey implements Serializable {
@ManyToOne
@JoinColumn(name = "accountId")
private Account account;
@ManyToOne
@JoinColumn(name = "productId")
private Product product;
}
这是我的 Spring Data 存储库:
public interface PositionRepository extends JpaRepository<Position, PositionKey> {
@EntityGraph(value = "PositionKey.all", type = EntityGraphType.LOAD)
List<Position> findByPositionKeyAccountIn(Set<Account> accounts);
}
这会产生以下异常:
java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [positionKey.account] on this ManagedType
我希望在一个包含职位的联接语句中检索所有帐户和产品。如何执行此操作/引用嵌入的 ID 属性?
解决方案
如果可能的话,我建议以这种方式重构实体
@Entity
@NamedEntityGraph(name = "PositionKey.all",
attributeNodes = {@NamedAttributeNode("account"),
@NamedAttributeNode("product")
})
@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;
}
这样一个EmbeddedId
更容易使用。例如,当您尝试通过id获取实体时,您不需要创建包含两个实体的复杂键。
推荐阅读
- python - Python中的项目根文件夹路径
- python - 删除除第一行之外的所有满足条件的行
- autodesk-forge - 如何使用 Model Derivative API 检索周长和特定几何属性?
- javascript - React.js If 语句
- node.js - 如何使用 Adonis 发出 json-rpc 请求
- python - 如何在将数据保存为 csv 时自动从给定写入文件名?
- image - 如果 crossOrigin 是匿名的,为什么 MDN 上的 CORS 会阻止图像请求?
- sql - T-SQL 'IF' 条件无法识别列名
- javascript - 我正在尝试使用 ajax 将数组从控制器传递到 javascript,但它不作为数组返回,而是作为字符串返回
- azure-blob-storage - 在不同订阅上运行 Azure Logic 应用