简体   繁体   English

使用 Criteria Builder API 自定义查询加入 JPA

[英]Custom Query Join in JPA using Criteria Builder API

Currently I am doing it like this:目前我正在这样做:

List<Table1Entity> findAllMatchingEntities(Table1Entity table1Entity) {
    String queryString = "SELECT table1.* FROM table1 " 
                    + "JOIN table2 t2 ON table1.id=t2.table1_id";

    if (table1Entity.getName() != null) {
          queryString +=" where name like ?";                 
    }

    Query query = em.createNativeQuery(queryString, Table1Entity.class);

    if (table1Entity.getName() != null) {
        query.setParameter(1, table1Entity.getName())    
    }

    return query.getResultedList();
}

If I want to check more parameters in this join this will quickly turn into a lot of if statements and it would be really complicated to set parameters correctly.如果我想在这个连接中检查更多参数,这将很快变成很多 if 语句,并且正确设置参数会非常复杂。

I know I can check parameters with criteria Builder API like this:我知道我可以使用标准 Builder API 来检查参数,如下所示:

if(table1Entity.getName() != null) {
    table1EntitySpecification = (root, query, criteriaBuilder)
                         -> criteriaBuilder.like(
                            criteriaBuilder.lower(root
                           .get("name")),
                           ("%" + table1Entity.getName() + "%")
                           .toLowerCase());;
}

and after that get them all with:然后让他们全部获得:

findAll(table1EntitySpecification) with findAll from simpleJPARepository . findAll(table1EntitySpecification)和来自simpleJPARepositoryfindAll Now I can chain them together with .or or .and etc. and avoid setting the parameter and checking for null second time.现在我可以将它们与.or.and等链接在一起,避免第二次设置参数和检查null

But how do I do join with criteria APi ?但是我该如何加入criteria APi

I know I can have in my @Repository something like this:我知道我可以在我的@Repository中有这样的东西:

@Query(value = "SELECT table1.* FROM table1 JOIN table2 t2 ON table1.id=t2.table1_id", nativeQuery = true)
List<Table1Entity> findAllMatchingEntities(Table1Entity table1Entity);

But since name is optional (can be null ) I can't just leave it in @Query .但由于name是可选的(可以是null ),我不能只将它留在@Query中。

What is the best solution here to avoid using native query and in case of having to check many parameters to avoid using if statements?避免使用本机查询以及必须检查许多参数以避免使用if语句的最佳解决方案是什么?

I don't know if I fully get your question, but regarding the possibility of nulls, and using the CRUD repository, you can always do a null check before like:我不知道我是否完全得到你的问题,但关于空值的可能性,以及使用 CRUD 存储库,你总是可以像之前一样做一个 null 检查:

@Query(value = "SELECT table1.* FROM table1 JOIN table2 t2 ON table1.id=t2.table1_id WHERE table1.id is not null", nativeQuery = true)
List<Table1Entity> findAllMatchingEntities(Table1Entity table1Entity);

Depending on what you are trying to achieve, you can always compose the query with similar checks like (not related to your code):根据您要实现的目标,您始终可以使用类似的检查来编写查询,例如(与您的代码无关):

@Query("SELECT c FROM Certificate c WHERE (:id is null or upper(c.id) = :id) "
           + "and (:name is null or upper(c.name) = :name)")
List<Table1> findStuff(@Param("id") String id,
                       @Param("name") String name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM