I am new to web development. I have been trying to make an e commerce platform and using @ManyToMany
mapping between product
and wishlist
using wishlist_product
as the joining table.
Upon adding, I am not facing error, but the data is not been added in the join table. I have been trying to resolve this issue for 2 days now, but have been facing continuous failure. I have attached the code for both, wishlist
and product
below.
@Entity
@Table(name = "wishlist")
public class Wishlist {
@Id
@Column(name = "username")
/*
* @GeneratedValue(generator = "gen")
*
* @GenericGenerator(name = "gen", strategy = "foreign", parameters
* = @Parameter(name = "property", value = "login"))
*/
private String username;
@ManyToMany
@JoinTable(name = "wishlist_product",
joinColumns = @JoinColumn(name = "username"),
inverseJoinColumns = @JoinColumn(name = "product_id"))
private List<Electronics> product;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "electronics")
@DiscriminatorColumn(name = "product_Type", discriminatorType = DiscriminatorType.STRING)
public class Electronics {
@Id
@GenericGenerator(name = "CamIdGenerator", strategy = "com.virtusa.neuralhack.vlx.IdGenerator.CameraIdGenerator")
@GeneratedValue(generator = "CamIdGenerator")
// @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "product_id")
private String productId;
private String askingPrice;
@Column(name = "age")
private String howOld;
@Column(name = "model")
private String model;
@Column(name = "brand")
private String brand;
@Column(name = "description")
private String description;
transient private String email;
@ManyToOne
@JoinColumn(name = "username")
private User_Login user;
@ManyToMany
@JoinTable(name = "wishlist_product", joinColumns = @JoinColumn(name = "product_id"), inverseJoinColumns = @JoinColumn(name = "username"))
private List<Wishlist> wishlist;
public void addToWishlist(String email, String productId) {
Session currentSession = manager.unwrap(Session.class);
Wishlist wishlist = currentSession.get(Wishlist.class,email);
//System.out.println(wishlist.getUser());
Electronics product = currentSession.get(Electronics.class, productId);
System.out.println(product);
wishlist.addToWishlist(product);
//product.addAWishlist(wishlist);
//System.out.println(wishlist.getProduct().get(0));
currentSession.saveOrUpdate(wishlist);
}
Make sure you in the read-write transaction but not read-only.
And some advises:
cascade = CascadeType.ALL
with the many-to-many association due to CascadeType.ALL
includes CascadeType.REMOVE
that in the case of many-to-many leads to too many entities are removed