java - Spring Data Jpa 存储库使用片段如何应用查询提示和实体图
问题描述
从 Spring 5 开始,我们可以选择使用多个片段存储库来丰富我们的 JPA 存储库。
机制很简单:声明然后实现一个接口。
public interface CustomRepository<T,K> {
// my very special finder
T findByIdCustom(K id);
}
public class CustomRepositoryImpl implements CustomRepository<T,K> {
T findByIdCustom(K id) {
// retrieve the item
}
}
然后,像这样使用它:
public interface ItemRepository
extends JpaRepository<Item, Long>, CustomRepository<Item, Long> {
}
现在,假设我想设置一些查询提示和/或这样的实体图:
public interface ItemRepository extends JpaRepository<Item, Long>, CustomRepository<Item, Long>{
@QueryHints(value = { @QueryHint(name = "name", value = "value")},
forCounting = false)
@EntityGraph(value = "Item.characteristics")
Item findByIdCustom(Long id);
}
JpaRepository
由于我有一个自定义实现,因此这里忽略了在方法上运行良好的上述查询提示和实体图。
我的问题是:
如何将方法的元数据应用于底层查询?
解决方案
您所要求的不能使用注释来工作,因为它们旨在与 Spring 提供实现的存储库方法一起使用。对于自定义方法,Spring 必然会忽略它——因为您的自定义代码负责构建、配置和执行查询,所以 Spring 无法在中途神奇地干预并注入查询提示。本质上,如果您想要自定义查询方法的两组不同提示,则需要两种不同的实现。
但是,您可能会尝试使用这样的模板方法模式:
public interface CustomRepository<T,K> {
T findByIdCustom(K id);
Map<String, Object> getHintsForFindByIdCustom();
// you'll probably need a default implementation for getHintsForFindByIdCustom here, unless it's possible to make CustomRepositoryImpl abstract - not sure how Spring Data will behave in this case
}
public class CustomRepositoryImpl<T, K> implements CustomRepository<T,K> {
T findByIdCustom(K id) {
TypedQuery<T> query= em.createQuery("...");
getHintsForFindByIdCustom().forEach((key, value) -> query.setHint(key, value));
return query.getResultList().iterator().next();
}
}
public interface ItemRepository extends JpaRepository<Item, Long>, CustomRepository<Item, Long>{
default Map<String, Object> getHintsForFindByIdCustom() {
return Map.of("name", "value");
}
}
public interface UserRepository extends JpaRepository<User, Long>, CustomRepository<User, Long>{
default Map<String, Object> getHintsForFindByIdCustom() {
return Map.of("name", "some-other-value");
}
}
请注意,我没有尝试过上面的代码。如果它不起作用,那么也许您可以为CustomRepositoryImpl
每个实体类尝试单独的实现。
推荐阅读
- google-api - 需要说明如何应用 Google Sheets API 限制
- charts - 自 1 天以来,Google 图表在 Load 上抛出错误
- python - 应用 dbscan 后如何提取点云?
- python - 如何将特定工作表的excel保存到另一个excel工作表python中
- javascript - 在 ajax 表、jsp 和 servlet 中显示数据
- sql-server - 想要突出显示组中的每一行
- django - Django ORM:如何计算具有相同属性的对象?
- java - 如何将 Maven 添加到路径变量 windows 10?
- c - 如何用空格交换矩阵中的特定字符
- javascript - 在 Bootstrap 5.0 的选择菜单中搜索