gpt4 book ai didi

hibernate - SchemaGen 错误地生成了不必要的关联表

转载 作者:行者123 更新时间:2023-12-04 08:04:18 25 4
gpt4 key购买 nike

我有一个非常简单的 DB 模式,经典的 订单---(一对多)---> OrderItem <---(多对一)--- 产品
但是,当我使用 SchemaGen 生成 DDL 时,它会创建一个额外的层 orders_orders_item 和 product_orders_item ,实际上添加了一个冗余的关联表层:

create table orders (order_id bigint not null auto_increment, order_amout varchar(255), primary key (order_id)) engine=InnoDB;
create table orders_item (orders_item_id bigint not null auto_increment, order_amount integer, product_id bigint not null, primary key (orders_item_id)) engine=InnoDB;
create table orders_orders_item (OrderEntity_order_id bigint not null, orderItems_orders_item_id bigint not null) engine=InnoDB;
create table product (id bigint not null auto_increment, name varchar(255), price decimal(19,2), primary key (id)) engine=InnoDB;
create table product_orders_item (ProductEntity_id bigint not null, orders_orders_item_id bigint not null) engine=InnoDB;
似乎认为 之间存在多对多关联。订单订单项 ,以及 之间产品 orders_item .
为什么会这样?
我的实体类如下所示:
订单 :
@Entity
@Table(name = "orders")
public class OrderEntity {

@Id
@Column(name = "order_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@OneToMany
private List<OrderItemEntity> orderItems;

protected OrderEntity() {
}

public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public List<OrderItemEntity> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<OrderItemEntity> orderItems) {
this.orderItems = orderItems;
}
}
订单项目:
@Entity
@Table(name = "orders_item")
public class OrderItemEntity {

@Id
@Column(name = "orders_item_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "product_id", nullable = false, updatable = false)
private ProductEntity product = new ProductEntity();

@Column(name = "order_amount")
private int amount;

protected OrderItemEntity() {
}

public OrderItemEntity(ProductEntity product, int amount) {
super();
this.product = product;
this.amount = amount;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public ProductEntity getProduct() {
return product;
}
public void setProduct(ProductEntity product) {
this.product = product;
}
}
产品 :
@Entity
@Table(name = "product")
public class ProductEntity {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@OneToMany
private List<OrderItemEntity> orders = new ArrayList<>();

@Column(name = "name")
private String name;

@Column(name = "price")
private BigDecimal price;

protected ProductEntity() {
}

public ProductEntity(String name) {
this.name = name;
}

public ProductEntity(String name, BigDecimal price) {
super();
this.name = name;
this.price = price;
}

public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
}

最佳答案

  • orders_orders_item表:

  • 由于您尚未指定 @JoinColumn ,而不是只有一个外键,而是创建了一个带有两个外键的额外表。
    orders_orders_item (OrderEntity_order_id, orderItems_orders_item_id) 
    要解决这个问题,您应该添加 @JoinColumn注释到 OrderEntity .
    @Entity
    @Table(name = "orders")
    public class OrderEntity {

    //...

    @OneToMany
    @JoinColumn(name = "order_id")
    private List<OrderItemEntity> orderItems;
    }
    有了这个映射,只有一个 order_id列将添加到 orders_item table 。 orders_orders_item不会不必要地创建表
  • product_orders_item表:
  • orders_item之间存在双向关系和 product .您指定了 @JoinColumnorders_item边。这导致了 product_id 的创建 orders_item上的专栏表哪个是对的。
    但另一方面,由于您没有指定 mappedBy它是一个双向关系 DB 试图通过创建 product_orders_item 来建立链接。 table 。
    您应该添加 mappedBy归因于 orders field 。
    @Entity
    @Table(name = "product")
    public class ProductEntity {

    // ...

    @OneToMany (mappedBy="product")
    private List<OrderItemEntity> orders = new ArrayList<>();
    }
    这说明已经在 product 之间创建了双向关系。和 orders_item (无需映射 orders 字段)。不需要额外的表创建等。感谢 @JoinColumn , product_id列是在 orders_item 中创建的 table 。

    关于hibernate - SchemaGen 错误地生成了不必要的关联表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66302300/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com