gpt4 book ai didi

spring - Hibernate 以多对多关系创建两个表

转载 作者:行者123 更新时间:2023-12-01 23:54:43 26 4
gpt4 key购买 nike

这是我的 Product 实体类:

public class Product extends BaseEntity {
@Column
@ManyToMany()
private List<Customer> customers = new ArrayList<>();

@ManyToOne
private Supplier supplier;
}

这是我的 Customer 实体类:

public class Customer extends BaseEntity {

//Enum type to String type in database '_'
@Enumerated(EnumType.STRING)
@Column
private Type type;

@Column
@ManyToMany(targetEntity = Product.class)
private List<Product> products = new ArrayList<>();
}

当我运行我的 Spring boot 项目时,它会在我的数据库 (Mysql) 中创建 2 个单独的表:product_customercustomer_product 但我只需要一个。我该怎么做才能解决这个问题?

最佳答案

按如下方式更新您的类:

   public class Product {

@ManyToMany
@JoinTable(name="product_customer"
joinColumns=@JoinColumn(name="product_id"),
inverseJoinColumns=@JoinColumn(name="customer_id")
)
private List<Customer> customers = new ArrayList<>();
...
}
    public class Customer extends BaseEntity {

@ManyToMany
@JoinTable(name="product_customer"
joinColumns=@JoinColumn(name="customer_id"),
inverseJoinColumns=@JoinColumn(name="product_id")
)
private List<Product> products = new ArrayList<>();
...
}

关于spring - Hibernate 以多对多关系创建两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62967136/

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