@ManyToMany休眠映射问题

时间:2020-09-27 18:26:08

标签: spring-boot hibernate many-to-many

我是Web开发的新手。我一直在尝试建立一个电子商务平台,并使用@ManyToMany作为联接表,在productwishlist之间使用wishlist_product映射。

添加后,我没有遇到错误,但是未将数据添加到联接表中。我已经尝试解决此问题已有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);
}

enter image description here

2 个答案:

答案 0 :(得分:1)

您应该执行以下操作:

@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. @ManyToMany注释应映射bidirectional @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);
}

答案 1 :(得分:1)

确保您处于读写事务但不是只读事务。

一些建议:

  • 请勿将cascade = CascadeType.ALL与多对多关联使用,因为CascadeType.ALL包括CascadeType.REMOVE,如果多对多导致导致太多实体被删除
  • 在不需要特定顺序时,使用Set而不是List进行多对关联。