Java 找到多个具有给定标识符的行:1,用于类:mz.co.zonal.models.Product
我试图把所有的Java 找到多个具有给定标识符的行:1,用于类:mz.co.zonal.models.Product,java,hibernate,spring-boot,spring-mvc,spring-data-jpa,Java,Hibernate,Spring Boot,Spring Mvc,Spring Data Jpa,我试图把所有的产品都存入数据库,看起来不错,但我总是有这个完整性错误 找到多个具有给定标识符的行:1,用于类:mz.co.zonal.models.Product;嵌套异常为org.hibernate.HibernateException:找到多个具有给定标识符的行:1,用于类:mz.co.zonal.models.Product 我的课程是 public class Product implements Serializable { @Id @GeneratedValue(st
产品都存入数据库,看起来不错,但我总是有这个完整性错误
找到多个具有给定标识符的行:1,用于类:mz.co.zonal.models.Product;嵌套异常为org.hibernate.HibernateException:找到多个具有给定标识符的行:1,用于类:mz.co.zonal.models.Product
我的课程是
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;}
@实体
公共类品牌实现可序列化{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@NotNull
私有字符串名称;
@OneToMany(fetch=FetchType.EAGER,mappedBy=“brand”,cascade={CascadeType.ALL})
@杰索尼奥雷
私有列表产品;}
我不确定这是否是问题的唯一原因,但您已将产品
和类型
之间的关系映射为1:1,但数据库中的行都引用相同的类型id
1。数据库是否为空?可能已经有以前测试的记录了?;)不,我的数据库中有数据。两种产品
@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;}