使用重叠路径表达式按JPA Criteria Query过滤

时间:2018-04-16 09:23:52

标签: java hibernate jpa criteria-api

我正在使用Jpa / Hibernate在Spring Boot应用程序中访问MariaDB

我有这个客户模型,它是公司。一家公司可以由另一家公司所有。

import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

class customer {

  @Id
  private Long id;
  private String name;
  private Company company;
}

class Company {

  @Id
  private Long id;
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "ownedBy")
  private Company ownedBy;
}

我想过滤公司的所有客户以及公司拥有的公司的客户。

e.g。

  • Customer1属于公司(id:1)

  • Customer2属于Company2(id:2

  • Company2归Company1所有

  • 查询id = 1 - > [Customer1,Customer2]

我根据公司ID或拥有的公司ID来构建以下2个预测值以匹配客户。

Join join = root.join("company");
Path companyPath = join.get("id");
// creating this path lead to the other one not working any more
Path companyOwnedPath = join.join("ownedBy").get("id");
Predicate company = criteriaBuilder.equal(companyPath, id);
Predicate companyOwned = criteriaBuilder.equal(companyOwnedPath, id);

他们都独自工作,所以我试图将它们与一个或。

结合起来
criteriaBuilder.or(company, companyOwned);

但是我只得到与companyOwned匹配的结果。但我发现,当我声明companyOwnedPath时(即使没有谓词),company谓词也不再有效。

创建这样的路径时是否有副作用?

这是整个查询构建的代码。

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.transaction.annotation.Transactional;


public interface CustomerRepositoryCustom {

  List<Customer> getByCompany(Long id, int page, int size);
}

@Transactional(readOnly = true)
public class CustomerRepositoryImpl implements CustomerRepositoryCustom {

  @PersistenceContext
  private EntityManager entityManager;

  public List<Customer> getByCompany(Long id, int page, int size) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

    CriteriaQuery<Customer> query = criteriaBuilder.createQuery(Customer.class);
    Root<Customer> root = query.from(Customer.class);
    CriteriaQuery<Customer> select = query.select(root);

    query.where(buildCompanyPredicate(criteriaBuilder, root, id);

    TypedQuery<Customer> typedQuery = entityManager.createQuery(select);

    typedQuery.setFirstResult(page * size);
    typedQuery.setMaxResults(page);

    return typedQuery.getResultList();
  }

  private Predicate buildCompanyPredicate(CriteriaBuilder criteriaBuilder, Root root, Long id) {

    // company join
    Join join = root.join("company");   
    Path companyPath = join.get("id");    
    // creating this path lead to the other one not working any more
    Path companyOwnedPath = join.join("ownedBy").get("id");  
    Predicate company = criteriaBuilder.equal(companyPath, id);   
    Predicate companyOwned = criteriaBuilder.equal(companyOwnedPath, id); 
    return criteriaBuilder.or(company, companyOwned);
  }
}

由于

1 个答案:

答案 0 :(得分:1)

From.join()方法系列的默认连接类型为INNER JOIN,这意味着,如果公司不属于其他公司,则不会出现在结果中。你想要join.join("ownedBy", JoinType.LEFT)