我试图建立简单的一对多关系,但 hibernate 抛出错误,不知道该怎么办。
类产品:
public class Products {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne()
@JoinColumn(name = "user_id", foreignKey = @ForeignKey(name = "fk_user"))
private Users users;
}
和类(class)用户:
public class Users {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
@OneToMany()
@JoinColumn(name = "product_id", foreignKey = @ForeignKey(name = "fk_product_id"))
private List<Products> productsList = new ArrayList<>();
}
我收到错误:通过 JDBC 语句执行 DDL“alter table products dropconstraint fk_user”时出错
最佳答案
这是这种关系的一个有效示例:
抽屉类:
@OneToMany (mappedBy="drawer", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, orphanRemoval = true)
private Set<Pocket> pockets;
袖珍类:
@ManyToOne (fetch=FetchType.EAGER)
@JoinColumn(name = "id_drawer", nullable = false)
private Drawer drawer;
关于java - Hibernate OnetoMany 和 ManyToOne 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60837393/