亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法為第二種情況映射 OneToMany 和 ManyToOne?

無法為第二種情況映射 OneToMany 和 ManyToOne?

幕布斯6054654 2023-09-20 15:53:42
我正在嘗試存儲用戶發出的訂單的集合。一個用戶可能有多個訂單,但一個訂單只有一個用戶。這樣就形成了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() 等內容。
查看完整描述

1 回答

?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

“您的 SQL 語法有錯誤;請檢查與您的 MySQL 服務器版本對應的手冊以獲取正確的語法錯誤” Hibernate 4

問題是我將我的表命名為“訂單”。這是從上面的堆棧跟蹤中獲得的。


查看完整回答
反對 回復 2023-09-20
  • 1 回答
  • 0 關注
  • 100 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號