@ManyToMany 休眠映射的问题

Issues with @ManyToMany hibernate mapping

我是网络开发的新手。我一直在尝试制作一个电子商务平台,并在 productwishlist 之间使用 @ManyToMany 映射,使用 wishlist_product 作为加入 table.

添加后,我没有遇到错误,但数据未添加到联接中 table。我已经尝试解决这个问题 2 天了,但一直面临着持续的失败。我在下面附上了 wishlistproduct 的代码。

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

你应该这样做:

@Entity
@Table(name = "wishlist")
public class Wishlist {

    @Id
    @Column(name = "username")
    private String username;

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name = "wishlist_product", 
                joinColumns = @JoinColumn(name = "username"),
                inverseJoinColumns = @JoinColumn(name = "product_id"))
    private List<Electronics> products;
    
    public void addProduct(Electronics product) {
       products.add( product );
       product.getWishlists().add( this );
    }

    public void removeProduct(Electronics product) {
      products.remove( product );
      product.getWishlists().remove( this );
    }
}


@Entity
@Table(name = "electronics")
public class Electronics {

   @Id
   @Column(name = "product_id")
   private String productId;
   
   @ManyToMany(mappedBy = "products")
   private List<Wishlist> wishlists;
}
  1. 只有 bidirectional @ManyToMany 的拥有方应由 @JoinTable 注释映射。你应该在另一边使用mappedBy

  2. 如果你想传播持久化和其他状态,你应该在拥有方设置cascade 属性。

  3. 为了保持双方之间的同步性,最好提供用于添加或删除子实体的辅助方法。

您的 addToWishlist 方法将如下所示:

public void addToWishlist(String email, String productId) {
   Session currentSession = manager.unwrap(Session.class);
   Wishlist wishlist = currentSession.get(Wishlist.class,email);
   Electronics product = currentSession.get(Electronics.class, productId);
   wishlist.addProduct(product);
   currentSession.saveOrUpdate(wishlist);
}

请确保您在 read-write 交易中而不是 read-only。

还有一些建议:

  • 不要将 cascade = CascadeType.ALL 与 many-to-many 关联使用,因为 CascadeType.ALL 包含 CascadeType.REMOVE,在 many-to-many 的情况下会导致太多实体已删除
  • 当您不需要特定顺序时,to-many 关联使用 Set 而不是 List。