我正在嘗試存儲(chǔ)用戶發(fā)出的訂單的集合。一個(gè)用戶可能有多個(gè)訂單,但一個(gè)訂單只有一個(gè)用戶。這樣就形成了1-M關(guān)系。Items 和 ItemOrders 之間也存在相同的關(guān)系,ItemOrder 由一個(gè) Item 和一定數(shù)量的要訂購(gòu)的 Items 組成。因此,一個(gè) ItemOrder 有一個(gè) Item,但一個(gè) Item 可以有多個(gè) ItemOrder。在我的測(cè)試套件中,我成功地正確創(chuàng)建了 Items 和 ItemOrders。但是,當(dāng)我擴(kuò)展測(cè)試以創(chuàng)建用戶和訂單時(shí),我收到“訂單”的 SQLSyntaxErrorException...這很奇怪,因?yàn)樗鼞?yīng)該是實(shí)現(xiàn)這兩種結(jié)果的完全相同的過程...而且我不知道是什么我在這里做錯(cuò)了,有什么想法嗎?@Entitypublic class ItemEntity implements EntityInt { @Id @GeneratedValue(generator = "incrementor") @Column(name = "item_id", unique = true) public int id; @OneToMany(mappedBy="item") public Set<OrderItemEntity> orderItems = new HashSet<>();}@Entitypublic class OrderItemEntity implements EntityInt { @Id @GeneratedValue(generator = "incrementor") @Column(name = "order_item_id", unique = true) public int id; @Column(name = "amount", nullable = false) public int amount; @ManyToOne @JoinColumn(name="item_id", nullable = false) private ItemEntity item; public OrderItemEntity(ItemEntity item, int amount) { this.setItem(item); this.amount = amount; } public void setItem(ItemEntity item) { if (this.item != null) this.item.orderItems.remove(this); this.item = item; this.item.orderItems.add(this); }}@Entitypublic class OrderEntity implements EntityInt { @Id @GeneratedValue(generator = "incrementor") @Column(name = "order_id", unique = true) public int id; @ManyToOne @JoinColumn(name="user_id", nullable = false) public UserEntity user; public UserEntity getUser() { return user; } public void setUser(UserEntity user) { if (this.user != null) this.user.orders.remove(this); this.user = user; this.user.orders.add(this); }}EntityInt 包含主要由 BasicDao 用于執(zhí)行 CRUD 操作的方法。它有 getId()、getVersion()、createVerifyIsUnqieuQuery() 等內(nèi)容。
無(wú)法為第二種情況映射 OneToMany 和 ManyToOne?
幕布斯6054654
2023-09-20 15:53:42