我试图得到所有产品
到数据库,这似乎很好,但总是我有这个完整性的错误。
找到具有给定标识符的多行:1,for class:mz.co. zonal.model.Products;嵌套异常是org.hibernate.HibernateException:找到具有给定标识符的多行:1,for class:mz.co.zonal.model.Products
我的课程是
public class Product implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String title;
@NotNull
private String description;
@NotNull
private double price;
@ManyToOne(fetch = FetchType.EAGER)
private Category category;
private boolean sold;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
private Currency currency;
@ManyToOne
@CreatedBy
private User user;
@Nullable
@OneToMany(mappedBy = "product",
cascade = CascadeType.ALL, orphanRemoval = true)
private List<Images> images;
private Date createdDate = new Date();
@OneToMany(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "product")
private List<View> view;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="type_id")
private Type type;
private Long viewCount;
@ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.ALL})
@JoinColumn(name="brand_id")
private Brand brand;
@Nullable
@OneToMany(mappedBy = "product",
cascade = CascadeType.ALL, orphanRemoval = true)
private List<ProductLikes> productLikes;
@Nullable
@Column
@ElementCollection(targetClass=byte.class)
private List<byte[]> imagesByte;}
@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Category implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
private String name;
@NotNull
private String imagePath;
@OneToMany(cascade = CascadeType.ALL,
mappedBy = "category")
@JsonIgnore
private List<Product> product;
@Nullable
private byte[] categoryImage;}
@Entity
public class Currency implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String code;
@NotNull
private String currency;
@NotNull
private String region_country;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@OneToMany(mappedBy = "currency", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonIgnore
private List<Product> products;
}
@Entity
public class ProductLikes {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Boolean isLike;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
@JsonIgnore
private User user;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "product_id", nullable = false)
@JsonIgnore
private Product product;}
@Entity
public class Images implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String imagePath;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_id")
private Product product;
}
@Entity
public class Type implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String name;
@OneToOne(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "type")
@JsonIgnore
private Product product;
}
@Entity
public class User implements UserDetails, Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotEmpty
private String fullName;
@NotEmpty
@Email
@Column(unique = true)
private String email;
@NotNull
@Column(unique = true)
private int phoneNumber;
@NotEmpty
@Size(min = 5)
private String password;
private Date createAt = new Date();
@Nullable
private String picPath;
@Nullable
private String token;
@ManyToMany
@JoinTable(name = "user_roles", joinColumns = {@JoinColumn(
name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "role_id")})
private List<Role> roles;
@OneToOne(fetch = FetchType.EAGER,
cascade = CascadeType.ALL,
mappedBy = "user")
@JsonIgnore
private Product product;
@OneToOne(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "user")
private View view;
}
@Entity
public class View implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id", nullable = false)
@JsonIgnore
private User user;
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "product_id", nullable = false)
@JsonIgnore
private Product product;
}
@Entity
public class Brand implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String name;
@OneToMany(fetch=FetchType.EAGER, mappedBy="brand", cascade={CascadeType.ALL})
@JsonIgnore
private List<Product> products;}
我不确定这是否是问题的唯一原因,但是您已经将Products
和Type
之间的关系映射为1:1,但是数据库中的行都引用相同的type_id
1。