相关疑难解决方法(0)

限制JPQL中的结果数量

如何限制从数据库中检索的结果数量?

select e from Entity e /* I need only 10 results for instance */
Run Code Online (Sandbox Code Playgroud)

java jpa jpql

51
推荐指数
2
解决办法
5万
查看次数

在JPQL中可以选择EXISTS()吗?

我正在尝试运行一个查询,检查某些条件是否为真,并返回一个简单的布尔结果作为输出.让它有点棘手的是,其中一个条件是测试是否没有为一组标准返回结果.

我目前正在使用JPA-2.0和hibernate作为我的提供者,由MySQL支持.我已经得到一个在MySQL中运行良好的示例查询,但是当试图让它在JPQL中运行时,它会失败.MySQL查询看起来有点像这样:

Select exists(Select statement with criteria) 
  or not exists(Select statement with criteria);
Run Code Online (Sandbox Code Playgroud)

我也使用CASE获得了相同的输出,但由于JPQL不支持该语句.

无论如何,当我尝试在JPQL中使用类似的查询时,我得到错误:

"意外结束的子树"

根据我的理解,这意味着查询中缺少某些内容.有谁知道如何解决它?

jpa jpql java-ee jpa-2.0

18
推荐指数
3
解决办法
3万
查看次数

使用JpaSpecificationExecutor时使用QueryHint

我使用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")
    }, …
Run Code Online (Sandbox Code Playgroud)

java specification-pattern eclipselink spring-data

2
推荐指数
1
解决办法
5060
查看次数