java – Hibernate CriteriaBuilder连接多个表

前端之家收集整理的这篇文章主要介绍了java – Hibernate CriteriaBuilder连接多个表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用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实现这一目标?请帮忙…

提前致谢…….

解决方法

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,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添加到构建类路径一样简单.因为我现在没有任何这样的项目,所以我对以下内容不太确定(所以请稍等一下).将以下内容添加为依赖项可能就足够了:

<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>

猜你在找的Java相关文章