gpt4 book ai didi

java - Hibernate OnetoMany 和 ManyToOne 抛出异常

转载 作者:行者123 更新时间:2023-12-01 19:28:05 25 4
gpt4 key购买 nike

我试图建立简单的一对多关系,但 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/

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