首页 > 解决方案 > 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由于我有一个自定义实现,因此这里忽略了在方法上运行良好的上述查询提示和实体图。

我的问题是:

如何将方法的元数据应用于底层查询?

标签: javajpaspring-data-jpa

解决方案


您所要求的不能使用注释来工作,因为它们旨在与 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每个实体类尝试单独的实现。


推荐阅读