title: how to create how to create many to many relationship in typeorm, [NestJS]
body: <p>How can I save data in manytomany relationship??
  (user, book (MTM))
  here is a many-to-many relationship between the user and the book.
  My service is not correct.
  Also, my code doesn't work.
  The data is stored in the book table.</p>
  <p>I need your help, everything
  Thank you in advance.</p>
  <p>My Stack =&gt; NestJs, TypeORM, MySQL</p>
  <p>There are my entities.
  <a href="https://i.stack.imgur.com/eL03D.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>user.entity</p>
  <pre><code>@Entity('User')
  export class User {
      @PrimaryGeneratedColumn()
      id!: number;
  
      @Column()
      real_name!: string;
  
      @Column()
      nick_name!: string;
  
      @Column()
      @IsEmail()
      email!: string;
  
      @Column()
      password!: string;
  
      @Column()
      phone_number!: string;
  
      @Column()
      image_url: string;
  
      @BeforeInsert()
      async hashPassword() {
          this.password = await argon2.hash(this.password, {type: argon2.argon2id, hashLength: 40});
      }
  }
  </code></pre>
  <p>book.entity</p>
  <pre><code>@Entity('Book')
  export class Book {
      @PrimaryGeneratedColumn()
      id!: number;
  
      @Column()
      title: string;
  
      @Column()
      image_url: string;
  
      @Column()
      contents: string;
  
      @Column({ type: 'datetime'})
      datetime: string;
  
      @ManyToMany(() =&gt; User)
      @JoinTable()
      users: User[];
  }
  </code></pre>
  <p>book.controller.ts</p>
  <pre><code>@UseGuards(JwtAuthGuard)
      @Post('bpc')
      savebpc(@Req() req: any, @Query('title') bookTitle: string){
          return this.BookService.addBpc(req, bookTitle);
      }
  </code></pre>
  <p>book.service.ts</p>
  <pre><code>async addBpc(req: any, bookTitle: string): Promise&lt;any&gt;{
          const userId = req.user.id;
          const bookId = await getRepository('Book')
          .createQueryBuilder('book')
          .where({title:bookTitle})
          .getRawOne()
  
          if (!bookId){
              throw new NotFoundException('Not_found_book');
          }
  
          const user = await getRepository('User')
          .createQueryBuilder('user')
          .where({id: userId})
          .getRawOne()
  
  
          //bookId.user.push(user);
          //await this.bookRepository.save(bookId);
  
          let userdata = new User();
          userdata.id = user.user_id;
          userdata.real_name = user.user_real_name;
          userdata.nick_name = user.user_nick_name;
          userdata.email = user.user_email;
          userdata.password = user.user_password;
          userdata.image_url = user.user_image_url;
          console.log(userdata);
          
  
          let bookBpc = new Book();
          bookBpc.title = bookId.book_title;
          bookBpc.image_url = bookId.book_image_url;
          bookBpc.contents = bookId.book_contents;
          bookBpc.datetime = bookId.book_datetime;
          bookBpc.users = [user];
          console.log(bookBpc);
  
          await this.bookRepository.create([bookBpc]);
          return 'suceess';
      }
  </code></pre>