Many-to-one / one-to-many is a relation where A contains multiple instances of B, but B contains only one instance of A. Let's take for example User and Photo entities. User can have multiple photos, but each photo is owned by only one single user.
Here we added @OneToMany to the photos property and specified the target relation type to be Photo. You can omit @JoinColumn in a @ManyToOne / @OneToMany relation. @OneToMany cannot exist without @ManyToOne. If you want to use @OneToMany, @ManyToOne is required. However, the inverse is not required: If you only care about the @ManyToOne relationship, you can define it without having @OneToMany on the related entity. Where you set @ManyToOne - its related entity will have "relation id" and foreign key.
With cascades enabled you can save this relation with only one save call.
To load a user with photos inside you must specify the relation in FindOptions:
constuserRepository=dataSource.getRepository(User)constusers=awaituserRepository.find({ relations: { photos:true, },})// or from inverse sideconstphotoRepository=dataSource.getRepository(Photo)constphotos=awaitphotoRepository.find({ relations: { user:true, },})
Or using QueryBuilder you can join them:
constusers=await dataSource.getRepository(User).createQueryBuilder("user").leftJoinAndSelect("user.photos","photo").getMany()// or from inverse sideconstphotos=await dataSource.getRepository(Photo).createQueryBuilder("photo").leftJoinAndSelect("photo.user","user").getMany()
With eager loading enabled on a relation, you don't have to specify relations in the find command as it will ALWAYS be loaded automatically. If you use QueryBuilder eager relations are disabled, you have to use leftJoinAndSelect to load the relation.