Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 使用QueryDSL的多个联接_Jpa_Spring Data Jpa_Querydsl - Fatal编程技术网

Jpa 使用QueryDSL的多个联接

Jpa 使用QueryDSL的多个联接,jpa,spring-data-jpa,querydsl,Jpa,Spring Data Jpa,Querydsl,我正在尝试将QueryDSL与Spring数据JPA一起使用 我的数据库有3个表 1) 包含帐户信息,特别是帐户编号的帐户表。 2) PersonRole表,其中包含个人在帐户上的角色(如所有者)以及相应的帐号和个人ID号。 3) 有一排人的人表。他们的身份证号码、名字、姓氏等 我的实体如下所示: @Entity Account{ //...Other fields ommited // ** @OneToMany @JoinColumn(name = "ACCNT_NUMBER") List&

我正在尝试将QueryDSL与Spring数据JPA一起使用

我的数据库有3个表

1) 包含帐户信息,特别是帐户编号的帐户表。 2) PersonRole表,其中包含个人在帐户上的角色(如所有者)以及相应的帐号和个人ID号。 3) 有一排人的人表。他们的身份证号码、名字、姓氏等

我的实体如下所示:

@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{...}
@实体
帐目{
//…其他字段已被删除
// **
@独身癖
@JoinColumn(name=“账户编号”)
列出个人角色**
}
@实体
角色{
字符串角色;
//…其他字段已被删除
// **
@奥内托内
@JoinColumn(name=“PERSON\u ID”)
个人**
}
@实体
人{…}
我想按人员的名字和姓氏筛选我选择的帐户,然后使用这些帐户填充具有关联人员角色和人员的合同实体

我想我必须创建连接才能做到这一点。我尝试了很多东西,但总是出错。我已经创建了相应的qclass。我知道下面的代码是错误的,它不工作,但也许你可以看看我是否在正确的轨道上,并可能帮助我。非常感谢您的帮助。谢谢大家!

    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();
QAccount account=QAccount.account;
QPersonRole personRoles=QPersonRole.personRole;
QPerson-person=QPerson.person;
JPAQuery query=新的JPAQuery(entityManager);
List accountList=查询
.来自(账户)
.innerJoin(account.personRoles,personRoles)
.innerJoin(个人)
.where(person.lastName.eq(“约翰”)
和(person.lastName.eq(“Doe”))
.fetch();

仅当您要在一对多上进行筛选时才需要联接。。。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();
QAccount account=QAccount.account;
QPersonRole personRole=QPersonRole.personRole;
JPAQuery query=新的JPAQuery(entityManager);
List accountList=查询
.来自(账户)
.innerJoin(acccount.personRoles,personRole)
.where(personRole.person.lastName.eq(“约翰”)
.和(personRole.person.lastName.eq(“Doe”))
.fetch();

请注意,如何不需要加入到个人中。

只有当您想筛选一对多时才需要加入。。。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();
QAccount account=QAccount.account;
QPersonRole personRole=QPersonRole.personRole;
JPAQuery query=新的JPAQuery(entityManager);
List accountList=查询
.来自(账户)
.innerJoin(acccount.personRoles,personRole)
.where(personRole.person.lastName.eq(“约翰”)
.和(personRole.person.lastName.eq(“Doe”))
.fetch();

请注意,如何不需要加入人员。

您所说的
合同是什么意思?问题中没有
合同的代码。另外,为什么从
PersonRole
Person
之间存在
@OneToOne
关联?据推测,一个
可以在多个
账户
中扮演角色,这意味着多个
人角色
记录,每个
账户都有一个
的角色。因此,从
PersonRole
Person
应该有一个
@ManyToOne
关联。如果不是,为什么不将角色名称和帐户引用存储在
人员
本身中呢?对不起,我指的是帐户而不是合同。我编辑。实际上,帐户和人员之间存在多对多关系,为了解决这个问题,我们使用了一个联接表。虽然一个人可以在一份合同中扮演多个角色,也可以在不同的合同中扮演多个角色,但我认为,通过将其设置为一个角色,可以为合同中的每个角色找到一个人。我应该改成onetomany吗?你所说的合同是什么意思?问题中没有
合同的代码。另外,为什么从
PersonRole
Person
之间存在
@OneToOne
关联?据推测,一个
可以在多个
账户
中扮演角色,这意味着多个
人角色
记录,每个
账户都有一个
的角色。因此,从
PersonRole
Person
应该有一个
@ManyToOne
关联。如果不是,为什么不将角色名称和帐户引用存储在
人员
本身中呢?对不起,我指的是帐户而不是合同。我编辑。实际上,帐户和人员之间存在多对多关系,为了解决这个问题,我们使用了一个联接表。虽然一个人可以在一份合同中扮演多个角色,也可以在不同的合同中扮演多个角色,但我认为,通过将其设置为一个角色,可以为合同中的每个角色找到一个人。我应该把它改成“一个女人”吗?先生,你是个英雄。谢谢你的帮助!现在我只需要将它与BooleanBuilder结合起来,我还想使用Spring数据分页。如果您对连接这些点有什么建议,我很想听听:)在查询dsl中进行连接时,您将无法使用spring数据的分页。限制和抵消是你的朋友。如果你能想出如何使用spring数据进行连接,那么让我知道上面的where子句中是否有一个拼写错误,
lastName
被使用了两次?先生,你是个英雄。谢谢你的帮助!现在我只需要将它与BooleanBuilder结合起来,我还想使用Spring数据分页。如果您对连接这些点有什么建议,我很想听听:)在查询dsl中进行连接时,您将无法使用spring数据的分页。限制和抵消是你的朋友。如果您能找到如何使用spring数据进行连接,那么请告诉我上面的where子句中是否有输入错误,其中
lastName