我正在嘗試存儲用戶發出的訂單的集合。一個用戶可能有多個訂單,但一個訂單只有一個用戶。這樣就形成了1-M關系。Items 和 ItemOrders 之間也存在相同的關系,ItemOrder 由一個 Item 和一定數量的要訂購的 Items 組成。因此,一個 ItemOrder 有一個 Item,但一個 Item 可以有多個 ItemOrder。在我的測試套件中,我成功地正確創建了 Items 和 ItemOrders。但是,當我擴展測試以創建用戶和訂單時,我收到“訂單”的 SQLSyntaxErrorException...這很奇怪,因為它應該是實現這兩種結果的完全相同的過程...而且我不知道是什么我在這里做錯了,有什么想法嗎?@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 用于執行 CRUD 操作的方法。它有 getId()、getVersion()、createVerifyIsUnqieuQuery() 等內容。
無法為第二種情況映射 OneToMany 和 ManyToOne?
幕布斯6054654
2023-09-20 15:53:42