【发布时间】:2020-07-18 09:53:02
【问题描述】:
这是我的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_customer 和 customer_product 但我只需要一个。我该怎么做才能解决这个问题?
【问题讨论】:
-
没有
@JoinTable,这不是双向关系,而是双方的两个单向关系,这就是创建两个表的原因。尝试用@JoinTable定义连接表
标签: spring spring-boot hibernate jpa spring-data-jpa