使用JpaSpecificationExecutor时,可以使用QueryHint。

7

我使用Spring Data和JpaSpecificationExecutor :: findAll方法获取我的模型。如何在调用此方法时使用查询提示?
上面的源代码运行良好,但我无法为我的JPA提供程序(在我的情况下是EclipseLink)设置QueryHint。

@Repository
public interface ProductRepository extends JpaRepository<Product, Integer>, JpaSpecificationExecutor<Product> {
}

@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;

    public List<Product> findByTitle(String locale, String titleToSearch) {
        return productRepository.findAll((Root<ProductCategory> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
            return builder.equal(builder.function("jsonb_extract_path_text", String.class, root.<String>get("title"), builder.literal(locale)), titleToSearch);
        });
    }
}

我使用spring-data来使用查询提示的方法如上所述。
@Repository
public interface ProductRepository extends JpaRepository<Product, Integer>, JpaSpecificationExecutor<Product> {

    @QueryHints(value = {
        @QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH_TYPE, value = "JOIN"),
        @QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH, value = "p.productCategory"),
        @QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH, value = "p.productFileList")
    }, forCounting = false)
    @Query("SELECT p FROM Product p")
    public List<Product> find();
}

我也发现了这个问题,目前还没有解决。


你尝试过在ProductRepository接口中覆盖findAll并添加查询提示吗? - Simon Martinelli
ProductRepository 接口在运行时被 Spring Data 实现(Spring 字节码注入)。Spring Data 支持使用在 Spring Data 文档中描述的过程来进行自定义实现。我将发布一个答案,因为这是我的解决方案。 - Georgios Syngouroglou
1个回答

24

当我想使用spring-data创建查询时,我遵循以下算法:

1) 查询是否已经由spring-data现有接口提供,例如CrudRepositoryPagingAndSortingRepositoryJpaRepository等?
示例: saveAndFlushfindAll 方法,更多详细信息请参见文档

Product product = new Product();
// Setters..
productRepository.saveAndFlush();

2) 我可以在方法名中使用关键字吗?
例如:count,更多请参考文档

@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {

    Long countByTitle(String title);

    List<Product> findByTitleLikeAndVisible(String title, boolean visible);
}

3) 我可以使用JPQL编写自定义查询方法吗?
示例: 文档.
在这种情况下,Spring Data不会尝试使用方法名称中的关键字创建查询,因此方法名称可以任意。

@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {

    @Query("SELECT COUNT(p) FROM Product p WHERE p.title=?1")
    Long countByTitle(String title);

    @Query("SELECT p FROM Product p WHERE p.title LIKE :title AND visible=true")
    List<Product> findByTitleLikeAndVisibleTrue(@Param("title") String title);
}

4) 我想要变量列名还是变量where条件?那么解决方案就是Specification。
例如:文档so答案

@Repository
public interface ProductRepository extends JpaRepository<Product, Integer>, JpaSpecificationExecutor<Product> {
}


@Service
public class ProductService {

    @Autowired
    private ProductRepository productRepository;

    public List<Product> findByColumn(String columnName, Object value) {
        return productRepository.find((Root<Product> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
            return builder.and(builder.equal(root.<String>get(columnName), value));
        });
    }
}

5) 我需要更多吗? 解决方案是获取EntityManager并像没有使用Spring Data库一样使用它。(这是对这个so问题的回答)
示例:so答案,更多信息请参见文档

// Create an interface and add the methods you wish to use with EntityManger.
public interface ProductRepositoryExt {
    public List<Product> findByTitle(String title);
}

// Implement the interface you created. Be careful the class name must be identical to the spring-data @Repository interface with the "Impl" appended.
public class ProductRepositoryImpl implements ProductRepositoryExt {

    @PersistenceContext
    private EntityManager em;

    @Override
    public List<Product> findByTitle(String title) {
//        em.getTransaction().begin();
        String sql = "SELECT p FROM Product p WHERE p.title=:title')";
        TypedQuery<ProductCategory> query = em.createQuery(sql, Product.class);
        query.setParameter("title", title);
        //  Add the query hints you wish..
        query.setHint(org.eclipse.persistence.config.QueryHints.BATCH_TYPE, "JOIN");
        query.setHint(org.eclipse.persistence.config.QueryHints.BATCH, "p.productCategory");

        return query.getResultList();
//        em.getTransaction().commit();
    }
}

// Extend this interface from your spring-data @Repository interface.
@Repository
public interface ProductRepository extends JpaRepository<Product, Integer>, ProductCategoryRepositoryExt {
}

2
现在针对当前Spring Data 2.2的解决方案4),给出一个小提示:方法find现在已更改为findAll - Mr. Anderson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接