【发布时间】:2021-01-13 10:17:12
【问题描述】:
我是网络开发的新手。我一直在尝试做一个电子商务平台,并使用product和wishlist之间的@ManyToMany映射,使用wishlist_product作为连接表。
添加后,我没有遇到错误,但数据未添加到连接表中。我已经尝试解决此问题 2 天了,但一直面临持续失败。我在下面附上了wishlist 和product 的代码。
@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);
}
【问题讨论】:
标签: spring-boot hibernate many-to-many