我使用弹簧数据和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();
}
我也发现了这个尚未解决的问题。