【发布时间】:2020-02-25 21:53:38
【问题描述】:
我正在尝试将所有 Product 放入数据库,看起来不错,但我总是有这个完整性错误。
More than one row with the given identifier was found: 1, for class: mz.co.zonal.models.Product; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: 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;}
@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;}
【问题讨论】:
-
数据库是空的吗?也许已经有以前测试的记录? ;)
-
不,我的数据库中有数据。两个产品i.ibb.co/4t9YzTr/zonall.png
标签: java hibernate spring-boot spring-mvc spring-data-jpa