Spring Boot Data Self通过Extras列多次刷新

时间:2018-09-24 12:16:49

标签: java spring hibernate spring-data-jpa many-to-many

我有一个案例,女巫是要使用Extras列实现自我连接多对多关系。 场景是我有产品(foo)可能由其他产品(bar1和bar 2)组成,女巫表示一个产品可以由许多产品组成<-------->产品可以由许多产品组成

我试图像这样实现它,但似乎根本没有用。

import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
import java.io.Serializable;
import java.util.Objects;

@Embeddable
public class ProductCompositionid implements Serializable {

    @ManyToOne()
    private Product productMaster;
    @ManyToOne()
    private Product productDetails;



    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass())
            return false;
        ProductCompositionid that = (ProductCompositionid) o;
        return Objects.equals(that.productDetails, this.productDetails) &&
                Objects.equals(that.productMaster, this.productMaster);
    }

    @Override
    public int hashCode() {
        return Objects.hash(productMaster, productDetails);
    }


}

加入表格

import javax.persistence.*;
import java.util.Objects;


@Entity
@Table(name = "composition")
@AssociationOverrides({
        @AssociationOverride(name = "productCompositionid.productMaster",
                joinColumns = @JoinColumn(name = "idproduct")),
        @AssociationOverride(name = "productCompositionid.productDetails",
                joinColumns = @JoinColumn(name = "childidproduct"))})
@Access(AccessType.PROPERTY)
public class ProductComposition {


    @EmbeddedId
    @Access(AccessType.FIELD)
    ProductCompositionid productCompositionid = new ProductCompositionid();
    private Double qty;

    public ProductComposition() {
    }

    public ProductComposition(ProductCompositionid productCompositionid) {
        this.productCompositionid = productCompositionid;
    }

    public ProductCompositionid getProductCompositionid() {
        return productCompositionid;
    }

    public void setProductCompositionid(ProductCompositionid productCompositionid) {
        this.productCompositionid = productCompositionid;
    }


    @Column(name = "qty")
    public Double getQty() {
        return qty;
    }

    public void setQty(Double qty) {
        this.qty = qty;
    }


    @Transient
    public Product getProductMaster() {
        return getProductCompositionid().getProductMaster();
    }

    public void setProductMaster(Product productMaster) {
        getProductCompositionid().setProductMaster(productMaster);
    }

    @Transient
    public Product getProductDetails() {
        return getProductCompositionid().getProductDetails();
    }

    public void setProductDetails(Product productDetails) {
        getProductCompositionid().setProductDetails(productDetails);
    }


    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof ProductComposition)) {
            return false;
        }
        ProductComposition that = (ProductComposition) o;
        return
                Objects.equals(this.productCompositionid, that.productCompositionid)
                ;
    }

    @Override
    public int hashCode() {
        return Objects.hash(productCompositionid, qty);
    }
}

实体

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Entity
@Table(name = "Product")
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idproduct")
    private Long idProduct;
    @Column(name = "namefr")
    private String nameFR;


    @OneToMany(fetch = FetchType.EAGER, mappedBy = "productCompositionid.productDetails", cascade = {CascadeType.ALL}, orphanRemoval = true)
    private Set<ProductComposition> detailsComposition = new HashSet<>();

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "productCompositionid.productMaster", cascade = {CascadeType.ALL}, orphanRemoval = true)
    private Set<ProductComposition> masterCompotion = new HashSet<>();


    @Override
    public String toString() {
        return nameFR;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof Product)) {
            return false;
        }

        Product that = (Product) o;


        return idProduct == that.idProduct &&

                Objects.equals(nameFR, that.nameFR)

                ;
    }

    @Override
    public int hashCode() {
        return Objects.hash(idProduct, nameFR
        );
    }


}

那么,实现这种情况的正确方法是什么。

0 个答案:

没有答案
相关问题