More than one row with the given identifier was found: 1, for class: mz.co.zonal.models.Product

I'm trying to get All Product into database, it seems well but always i have this error of integrity.

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

My Classes is

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;}

Upvotes: 1

Views: 189

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81882

I'm not sure if this is the only cause of problems, but you have mapped the relation between Product and Type as 1:1 but the rows in the database both reference the same type_id 1.

Upvotes: 1

Related Questions