使用JpaSpecificationExecutor时使用QueryHint

Geo*_*lou 2 java specification-pattern eclipselink spring-data

我使用spring数据和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);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

以上是我使用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();
}
Run Code Online (Sandbox Code Playgroud)

我已经还发现,是尚未解决的呢。

Geo*_*lou 13

当我想使用spring-data创建查询时,请遵循上述算法。

1)是否已经提供的查询通过弹簧数据等的现有接口CrudRepositoryPagingAndSortingRepositoryJpaRepository等?
示例:saveAndFlushfindAll方法,更多内容参见docs

Product product = new Product();
// Setters..
productRepository.saveAndFlush();
Run Code Online (Sandbox Code Playgroud)

2)我可以在方法名称中使用关键字创建方法吗?
示例:count,更多内容在docs中

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

    Long countByTitle(String title);

    List<Product> findByTitleLikeAndVisible(String title, boolean visible);
}
Run Code Online (Sandbox Code Playgroud)

3)是否可以创建编写JPQL的自定义查询方法?
示例:docs
在这种情况下,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);
}
Run Code Online (Sandbox Code Playgroud)

4)我要变量列名还是变量在哪里条件?那么解决方案就是规范。
示例: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));
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

5)我想要更多吗?解决方案是获取EntityManager并像在没有spring数据库的情况下使用它一样使用它。(这是这个问题的答案)
示例: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 {
}
Run Code Online (Sandbox Code Playgroud)

  • 只是当前 Spring Data 2.2 中关于 **解决方案 4)** 的一个提示:方法 **find** 现在是 **findAll**。 (2认同)

归档时间:

查看次数:

5060 次

最近记录:

8 年,4 月 前