[英]Use QueryHint when using JpaSpecificationExecutor
I use spring data and the JpaSpecificationExecutor::findAll
method to fetch my models. 我使用spring数据和JpaSpecificationExecutor::findAll
方法来获取我的模型。 How I could use query hints when calling this method? 调用此方法时如何使用查询提示?
The above source code works fine but I can't set QueryHint for my JPA provider (in my case EclipseLink). 上面的源代码工作正常但我不能为我的JPA提供程序设置QueryHint(在我的例子中是EclipseLink)。
@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);
});
}
}
The way I use the Query Hints using spring-data is the above, 我使用spring-data使用Query Hints的方法如上,
@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();
}
I 've also found this which is unresolved yet. 我还发现了这个尚未解决的问题。
When I want to create a query using spring-data I follow the above algorithm. 当我想使用spring-data创建查询时,我遵循上面的算法。
1) Is the query already provided by the existing interfaces of spring-data like
CrudRepository
,PagingAndSortingRepository
,JpaRepository
etc? 1)查询是否已由spring-data的现有接口提供 ,如CrudRepository
,PagingAndSortingRepository
,JpaRepository
等?
Examples:saveAndFlush
orfindAll
methods, more in docs . 示例:saveAndFlush
或findAll
方法,更多在文档中 。
Product product = new Product();
// Setters..
productRepository.saveAndFlush();
2) Can I create a method using keywords inside method names? 2)我可以使用方法名称中的关键字创建方法吗?
Examples: count , more in docs . 示例: 计数 ,更多在文档中 。
@Repository
public interface ProductRepository extends JpaRepository<Product, Integer> {
Long countByTitle(String title);
List<Product> findByTitleLikeAndVisible(String title, boolean visible);
}
3) Can I create a custom query method writing JPQL? 3)我可以创建一个编写JPQL的自定义查询方法吗?
Examples: docs . 示例: docs 。
In this case spring data does not try to create the query using keywords inside method names, so the method names can be whatever you wish. 在这种情况下,spring数据不会尝试使用方法名称中的关键字创建查询,因此方法名称可以是您想要的任何名称。
@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) Do I want variable column names or variable where conditions? 4)我想要变量列名称或变量的条件吗? Then the solution is the Specification. 然后解决方案是规范。
Example: docs , so answer 示例: docs , 所以回答
@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) Do I want more? 5)我想要更多吗? The solution is to get the EntityManager and use it like I used it without the spring data library. 解决方案是获取EntityManager并像使用它一样使用它而不使用spring数据库。 (This is the answer to this so question) (这就是这个问题的答案)
Example: so answer , more in docs 示例: 所以回答 ,更多在文档中
// 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 {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.