TypeOrm 根据与另一个实体的关系查找行
我有一个 PersonEntity,一个 Person 可以有很多 Items,其中一些 Items 可以标记为“豪华”(ItemEntity 上的豪华属性)。我怎样才能只获得拥有所有豪华物品的人?
@Entity({name: 'persons'})
export class PersonEntity {
@Column({nullable: false})
name: string;
/** Relationships **/
@OneToMany(type => ItemEntity, item => item.person)
items: ItemEntity[];
}
@Entity({name: 'items'})
export class ItemEntity {
@Column({nullable: false})
name: string;
@Column({nullable: false})
deluxe: boolean;
@Column({nullable: false})
person_id: number;
/** Relationships **/
@ManyToOne(type => PersonEntity, person => person.items)
@JoinColumn({name: 'person_id'})
person: PersonEntity;
}
I have a PersonEntity, and a Person can have many Items, some of those Items can be marked as ‘deluxe’ (deluxe property on ItemEntity). How can I get only the Persons that have all of their Items deluxe ?
@Entity({name: 'persons'})
export class PersonEntity {
@Column({nullable: false})
name: string;
/** Relationships **/
@OneToMany(type => ItemEntity, item => item.person)
items: ItemEntity[];
}
@Entity({name: 'items'})
export class ItemEntity {
@Column({nullable: false})
name: string;
@Column({nullable: false})
deluxe: boolean;
@Column({nullable: false})
person_id: number;
/** Relationships **/
@ManyToOne(type => PersonEntity, person => person.items)
@JoinColumn({name: 'person_id'})
person: PersonEntity;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以这样实现它(使用
EntityManager
或Person
存储库):You can achieve it like that (using
EntityManager
orPerson
repository) :