当前位置: 首页 > 知识库问答 >
问题:

与QueryDSL的多个联接

阎建德
2023-03-14

我试图将QueryDSL与Spring Data JPA一起使用。

@Entity
Account{
//...Other fields ommited
// **
@OneToMany
@JoinColumn(name = "ACCNT_NUMBER")
List<PersonRole> personRoles;**
}

@Entity
PersonRole{
String role;
// ...Other fields ommited
// **
@OneToOne
@JoinColumn(name = "PERSON_ID")
Person person;**
}


@Entity
Person{...}
    QAccount account = QAccount.account;
    QPersonRole personRoles = QPersonRole.personRole;
    QPerson person = QPerson.person;

    JPAQuery<Account> query = new JPAQuery<>(entityManager);

    List<Account> accountList = query
            .from(account)
            .innerJoin(acccount.personRoles, personRoles)
            .innerJoin(person)
            .where(person.lastName.eq("John")
                    .and(person.lastName.eq("Doe")))
            .fetch();

共有1个答案

狄飞尘
2023-03-14

只有当你想从一个到多个过滤时才需要联接...jpql仍然可以被利用...我更喜欢在personRole上使用单数,所以:

QAccount account = QAccount.account;
QPersonRole personRole = QPersonRole.personRole;

JPAQuery<Account> query = new JPAQuery<>(entityManager);

List<Account> accountList = query
            .from(account)
            .innerJoin(acccount.personRoles, personRole)
            .where(personRole.person.lastName.eq("John")
                    .and(personRole.person.lastName.eq("Doe")))
            .fetch();

请注意如何不需要连接到Person。

 类似资料:
  • null 类似于REST查询DSL 用于全文和结构化搜索的查询语言

  • 假设我有两个表和。包含和列。有两列和,它们链接回的列。

  • Hibernate5.2.10 查询DSL(jpa,apt)4.1.4 Spring Boot 1.5.6 Spring Data JPA(因此,使用存储库) 存储库扩展 选择以下meetupCampaign.id=x *meetupCampaign.ApprovalStatus=y的关联会议: *meetupCampaign.id=x *meetupCampaign.ApprovalStatus

  • > 帐户表:accountId(PK)电子邮件密码 account_profile表:accountId(PK)(fk到account)昵称 团体表:articleId(PK)accountId(fk to account)标题内容 现在我想要下面的JPQL是查询DSL代码 我有实体元模型-QAccount、QAccountProfile、QCommunity 此外,我必须通过分页获得结果,因此应

  • 我只想检查一下QueryDSL版本3.1.1。-是否仍然不可能与子查询连接,就像这里的答案所写的:JPQL/querydsl:join subquery and get aliased column

  • 我有两个多对多关联的表。 DB详细信息:用户-->列[Id,name]