TypeOrm保存一对多对象关系

时间:2019-11-08 21:39:37

标签: mysql node.js typeorm

当尝试保存具有一对多关系的对象(mysql)时遇到问题,它实际上只保存顶层实体。生成的查询是

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ... liveData= cvViewModel.getNewsRepository() liveData?.observe(this, Observer { items -> //Do your operation here }) }

我不知道做错了什么

应该为事务下的每个实体生成三个查询 对不起,因为我是Node和TypeORM的新手,可能我错过了smth SeatsTemplate,ZoneTempalte和SeceneTemlate是实体,已成功加载。

SeceneTemplates.ts

INSERT INTO `SeceneTemplates`(`Id`, `Name`, `Address`, `Rows`, `Cols`) VALUES (DEFAULT, ?, ?, ?, ?) -- PARAMETERS: ["Pallati i Kongreseve","Sheshi Nene Tereza",5,5]

ZoneTemaplate.ts

import { ZoneTemplates } from "./ZoneTemplates";
import { Column, PrimaryGeneratedColumn, Entity, OneToMany } from "typeorm";

@Entity("SeceneTemplates")
export class SeceneTemplates {
  @PrimaryGeneratedColumn()
  Id: bigint;
  @Column()
  Name!: string;
  @Column()
  Address: string;
  @Column()
  Rows: number;
  @Column()
  Cols: number;

  @OneToMany(
    type => ZoneTemplates,
    zoneTemplate => zoneTemplate.sceneTemplate,
    { cascade: ["insert", "update", "remove"] }
  )
  zoneTemplates: ZoneTemplates[];

  public constructor(init?: Partial<SeceneTemplates>) {
    Object.assign(this, init);
  }
}

SeatsTemplates.ts

import { SeceneTemplates } from "./SeceneTemplates";
import { SeatsTemplates } from "./SeatsTemplates";
import {
  Column,
  PrimaryGeneratedColumn,
  Entity,
  ManyToOne,
  OneToMany
} from "typeorm";

@Entity("ZoneTemplates")
export class ZoneTemplates {
  @PrimaryGeneratedColumn()
  Id: bigint;
  @Column()
  Name!: string;
  @Column()
  Description: string;
  @Column()
  ColorCode: string;

  @ManyToOne(
    type => SeceneTemplates,
    sceneTemplate => sceneTemplate.zoneTemplates,
    { cascade: ["insert", "update", "remove"] }
  )
  sceneTemplate: SeceneTemplates;
  @OneToMany(
    type => SeatsTemplates,
    seatsTemplate => seatsTemplate.zoneTemplate,
    { cascade: ["insert", "update", "remove"] }
  )
  seatsTemplates: SeatsTemplates[];

  public constructor(init?: Partial<ZoneTemplates>) {
    Object.assign(this, init);
  }
}

SceneService

import { SeceneTemplates } from "./SeceneTemplates";
import { ZoneTemplates } from "./ZoneTemplates";
import {
  Entity,
  Column,
  PrimaryColumn,
  PrimaryGeneratedColumn,
  ManyToOne
} from "typeorm";

@Entity("SeatsTemplates")
export class SeatsTemplates {
  @PrimaryGeneratedColumn()
  Id: bigint;
  @Column()
  x: number;
  @Column()
  y: number;

  @ManyToOne(
    type => ZoneTemplates,
    zoneTemplate => zoneTemplate.seatsTemplates,
    { cascade: ["insert", "update", "remove"] }
  )
  zoneTemplate: ZoneTemplates;

  public constructor(init?: Partial<SeatsTemplates>) {
    Object.assign(this, init);
  }
}

0 个答案:

没有答案