我有一个与第二个实体有OneToOne关系的主实体:
@Entity('main_entity')
export class MainEntity {
@PrimaryGeneratedColumn('increment')
@IsInt()
id: number;
@OneToOne(
() => SecondEntity,
(second) => second.main,
)
second: SecondEntity;
}
我有第二个实体,它与第三个实体有一个OneToMany关系:
@Entity('second_entity')
export class SecondEntity {
@PrimaryGeneratedColumn()
@IsInt()
id: number;
@OneToOne(() => MainEntity)
@JoinColumn()
main: MainEntity;
@OneToMany(() => ThirdEntity, (third) => third.secondEntity)
third: ThirdEntity[];
}
我还有第三个实体,它与第二个ManyToOne关系相关:
export class ThirdEntity {
@PrimaryGeneratedColumn()
@IsInt()
id: number;
@Column()
@Length(1, 255)
name: string;
@ManyToOne(() => SecondEntity, (second) => second.id)
second: SecondEntity;
}
注意问题:在过滤响应中的数据时,我希望使用queryBuilder获得所有依赖的实体,但我想不出如何统一第三个实体。我如何从"主"中获得第三个实体?querybuilder如果主不知道任何关于第三?关系链是这样的:main knows only about second, second knows about main and third, and third knows only about second
const query = await this.createQueryBuilder('main')
.leftJoinAndSelect('main.second', 'second')
.leftJoinAndSelect('second.third', 'third')
我已经尝试了许多类似上面的选项,但都没有成功
错误是在别名的名称,我认为它不工作,因为ManyToOne关系,事实证明,我只是重新指定了相同的别名。