我试图使用hibernate criteriabuilder加入4个表。
下面分别是表格..
`
@Entity
public class BuildDetails {
@Id
private long id;
@Column
private String buildNumber;
@Column
private String buildDuration;
@Column
private String projectName;
}
@Entity
public class CodeQualityDetails{
@Id
private long id;
@Column
private String codeHealth;
@ManyToOne
private BuildDetails build; //columnName=buildNum
}
@Entity
public class DeploymentDetails{
@Id
private Long id;
@Column
private String deployedEnv;
@ManyToOne
private BuildDetails build; //columnName=buildNum
}
@Entity
public class TestDetails{
@Id
private Long id;
@Column
private String testStatus;
@ManyToOne
private BuildDetails build; //columnName=buildNum
}
在这4个表中,我想为MySQL执行以下sql脚本:
SELECT b.buildNumber, b.buildDuration,
c.codeHealth, d.deployedEnv, t.testStatus
FROM BuildDetails b
INNER JOIN CodeQualityDetails c ON b.buildNumber=c.buildNum
INNER JOIN DeploymentDetails d ON b.buildNumber=d.buildNum
INNER JOIN TestDetails t ON b.buildNumber=t.buildNum
WHERE b.buildNumber='1.0.0.1' AND
b.projectName='Tera'
那么,我如何使用Hibernate CriteriaBuilder实现这一目标?请帮忙...
在此先感谢.......
答案 0 :(得分:10)
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);
List<MyQueriedBuildDetails> resultList = typedQuery.getResultList();
我假设你为你的类构建了JPA元模型。如果您没有元模型或者您根本不想使用它,只需将BuildDetails_.buildNumber
替换为其余部分,并将列的实际名称替换为String
,例如"buildNumber"
。
请注意,我无法测试答案(也是在没有编辑器支持的情况下编写它),但它应该至少包含构建查询时需要知道的所有内容。
如何构建元模型?请查看hibernate tooling(或参考How to generate JPA 2.0 metamodel?了解其他替代方案)。如果您正在使用maven,那么只需将hibernate-jpamodelgen
- 依赖项添加到构建类路径即可。因为我现在没有任何这样的项目,所以我对以下内容不太确定(所以请稍等一下)。将以下内容添加为依赖项可能就足够了:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.3.7.Final</version>
<scope>provided</scope> <!-- this might ensure that you do not package it, but that it is otherwise available; untested now, but I think I used it that way in the past -->
</dependency>