使用Hibernate 3.6.10的CriteriaQuery

时间:2018-10-26 13:29:07

标签: hibernate criteria

我想使用CriteriaBuilder创建查询。 我正在使用hibernate 3.6.10

查询的结构将类似于以下内容  -How to join Multiple tables using hibernate criteria where entity relationship is not direct?

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery query = cb.createQuery(/* Your combined target type, e.g. MyQueriedBuildDetails.class, containing buildNumber, duration, code health, etc.*/);

Root<BuildDetails> buildDetailsTable = query.from(BuildDetails.class);
Join<BuildDetails, CopyQualityDetails> qualityJoin = buildDetailsTable.join(CopyQualityDetails_.build, JoinType.INNER);
Join<BuildDetails, DeploymentDetails> deploymentJoin = buildDetailsTable.join(DeploymentDetails_.build, JoinType.INNER);
Join<BuildDetails, TestDetails> testJoin = buildDetailsTable.join(TestDetails_.build, JoinType.INNER);

List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(BuildDetails_.buildNumber, "1.0.0.1"));
predicates.add(cb.equal(BuildDetails_.projectName, "Tera"));

query.multiselect(buildDetails.get(BuildDetails_.buildNumber),
                  buildDetails.get(BuildDetails_.buildDuration),
                  qualityJoin.get(CodeQualityDetails_.codeHealth),
                  deploymentJoin.get(DeploymentDetails_.deployedEnv),
                  testJoin.get(TestDetails_.testStatus));
query.where(predicates.stream().toArray(Predicate[]::new));

TypedQuery<MyQueriedBuildDetails> typedQuery = entityManager.createQuery(query);

但是由于我没有使用JPA,所以无法使用entityManager. 因此,有什么方法可以使用CriteriaQuery来构造Hibernate 3.6.10吗?

我正在用Hibernate 3.6.10.

来创建类似于上面的查询

1 个答案:

答案 0 :(得分:1)

在Hibernate 3.6中,您不能使用JPA Criteria API。

但是Hibernate有他自己的条件查询(在Hibernate 5中已弃用):

http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html_single/#querycriteria