与休眠的所有关系 ManyToOne 中的值为 null
Value null in all relations ManyToOne with hibernate
我想在产品和供应商之间建立1对N的关系,这样一个供应商可以提供多个产品,但一个产品只有一个供应商。
我已经使用了相应的 JPA 注释,但仍然没有建立正确的关系。获取我所有的关系 1 到 N,因为值输入为空。
发生了什么事?
Product.java
@Entity
@Table(name="PRODUCTS")
public class Product implements Serializable{
private static final long serialVersionUID = 1L;
@Id //@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private String name;
private String unit;
private double price;
private boolean available;
@ManyToOne (cascade = {
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.PERSIST,
CascadeType.REFRESH
})
@JoinColumn(insertable=false, updatable=false)
private Supplier supplier;
@OneToMany(mappedBy = "product", fetch = FetchType.EAGER, cascade = {
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.PERSIST,
CascadeType.REFRESH
})
private List<OrderDetail> orderDetails;
@ManyToMany
@JoinColumn(insertable=false, updatable=false)
private List<Category> categories;
@ManyToMany(mappedBy = "cart", fetch = FetchType.EAGER, cascade = {
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.PERSIST,
CascadeType.REFRESH
})
private List<Client> customers;
Supplier.java
@Entity
@Table(name="SUPPLIERS")
public class Supplier implements Serializable {
private static final long serialVersionUID = 1L;
@Id //@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private String name;
private String contactName;
private String email;
private String password;
private String phone;
private String country;
private String city;
private String address;
private String postalCode;
@OneToMany(mappedBy="supplier", fetch = FetchType.EAGER, cascade = {
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.PERSIST,
CascadeType.REFRESH
})
private List<Product> suppliedProducts;
我已经解决了将 @JoinColumn(insertable=false, updatable=false)
从 false
替换为 true
的问题。此更改允许休眠插入和更新所需的值。
我想在产品和供应商之间建立1对N的关系,这样一个供应商可以提供多个产品,但一个产品只有一个供应商。 我已经使用了相应的 JPA 注释,但仍然没有建立正确的关系。获取我所有的关系 1 到 N,因为值输入为空。 发生了什么事?
Product.java
@Entity
@Table(name="PRODUCTS")
public class Product implements Serializable{
private static final long serialVersionUID = 1L;
@Id //@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private String name;
private String unit;
private double price;
private boolean available;
@ManyToOne (cascade = {
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.PERSIST,
CascadeType.REFRESH
})
@JoinColumn(insertable=false, updatable=false)
private Supplier supplier;
@OneToMany(mappedBy = "product", fetch = FetchType.EAGER, cascade = {
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.PERSIST,
CascadeType.REFRESH
})
private List<OrderDetail> orderDetails;
@ManyToMany
@JoinColumn(insertable=false, updatable=false)
private List<Category> categories;
@ManyToMany(mappedBy = "cart", fetch = FetchType.EAGER, cascade = {
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.PERSIST,
CascadeType.REFRESH
})
private List<Client> customers;
Supplier.java
@Entity
@Table(name="SUPPLIERS")
public class Supplier implements Serializable {
private static final long serialVersionUID = 1L;
@Id //@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private String name;
private String contactName;
private String email;
private String password;
private String phone;
private String country;
private String city;
private String address;
private String postalCode;
@OneToMany(mappedBy="supplier", fetch = FetchType.EAGER, cascade = {
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.PERSIST,
CascadeType.REFRESH
})
private List<Product> suppliedProducts;
我已经解决了将 @JoinColumn(insertable=false, updatable=false)
从 false
替换为 true
的问题。此更改允许休眠插入和更新所需的值。