简体   繁体   中英

Spring boot Hibernate one to Many Relationship

I have two entities Product and Product Options. Product can have multiple product options and as it can be seen I have mapped it with oneToMany Relationship.

@Entity
public class Product {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String description;
    private String productCategory;
    private String optionDescription;
    private BigDecimal productBasePrice;
    @OneToMany(mappedBy = "product",cascade = CascadeType.ALL)
    private Set<ProductOption>productOptions=new HashSet<>();


    public Product() {}
    public Product(String description, ProductCategory productCategory,String optionDescription, BigDecimal productBasePrice) {
        super();
        this.description = description;
        this.productCategory=productCategory.toString();
        this.optionDescription = optionDescription;
        this.productBasePrice = productBasePrice;
    }


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getDescription() {
        return description;
    }


    public String getProductCategory() {
        return productCategory;
    }
    public void setProductCategory(String productCategory) {
        this.productCategory = productCategory;
    }
    public void setDescription(String description) {
        this.description = description;
    }


    public String getOptionDescription() {
        return optionDescription;
    }


    public void setOptionDescription(String optionDescription) {
        this.optionDescription = optionDescription;
    }


    public BigDecimal getProductBasePrice() {
        return productBasePrice;
    }


    public void setProductBasePrice(BigDecimal productBasePrice) {
        this.productBasePrice = productBasePrice;
    }


    public Set<ProductOption> getProductOptions() {
        return productOptions;
    }


    public void setProductOptions(Set<ProductOption> productOptions) {
        this.productOptions = productOptions;
    }


}

And ProductOptions

@Entity
public class ProductOption {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String productOptionDescription;
    private BigDecimal optionPrice;
    private BigDecimal optionPriceForSmall;
    private BigDecimal optionPriceForNormal;
    private BigDecimal optionPriceForFamily;
    private BigDecimal optionPriceForParty;
    @ManyToOne
    @JoinColumn 
    private Product product;

    public ProductOption() {}
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getProductOptionDescription() {
        return productOptionDescription;
    }
    public void setProductOptionDescription(String productOptionDescription) {
        this.productOptionDescription = productOptionDescription;
    }
    public BigDecimal getOptionPrice() {
        return optionPrice;
    }
    public void setOptionPrice(BigDecimal optionPrice) {
        this.optionPrice = optionPrice;
    }
    public BigDecimal getOptionPriceForSmall() {
        return optionPriceForSmall;
    }
    public void setOptionPriceForSmall(BigDecimal optionPriceForSmall) {
        this.optionPriceForSmall = optionPriceForSmall;
    }
    public BigDecimal getOptionPriceForNormal() {
        return optionPriceForNormal;
    }
    public void setOptionPriceForNormal(BigDecimal optionPriceForNormal) {
        this.optionPriceForNormal = optionPriceForNormal;
    }
    public BigDecimal getOptionPriceForFamily() {
        return optionPriceForFamily;
    }
    public void setOptionPriceForFamily(BigDecimal optionPriceForFamily) {
        this.optionPriceForFamily = optionPriceForFamily;
    }
    public BigDecimal getOptionPriceForParty() {
        return optionPriceForParty;
    }
    public void setOptionPriceForParty(BigDecimal optionPriceForParty) {
        this.optionPriceForParty = optionPriceForParty;
    }

    public ProductOption(String productOptionDescription, BigDecimal optionPrice, BigDecimal optionPriceForSmall,
            BigDecimal optionPriceForNormal, BigDecimal optionPriceForFamily, BigDecimal optionPriceForParty) {
        super();
        this.productOptionDescription = productOptionDescription;
        this.optionPrice = optionPrice;
        this.optionPriceForSmall = optionPriceForSmall;
        this.optionPriceForNormal = optionPriceForNormal;
        this.optionPriceForFamily = optionPriceForFamily;
        this.optionPriceForParty = optionPriceForParty;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((optionPrice == null) ? 0 : optionPrice.hashCode());
        result = prime * result + ((optionPriceForFamily == null) ? 0 : optionPriceForFamily.hashCode());
        result = prime * result + ((optionPriceForNormal == null) ? 0 : optionPriceForNormal.hashCode());
        result = prime * result + ((optionPriceForParty == null) ? 0 : optionPriceForParty.hashCode());
        result = prime * result + ((optionPriceForSmall == null) ? 0 : optionPriceForSmall.hashCode());
        result = prime * result + ((productOptionDescription == null) ? 0 : productOptionDescription.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ProductOption other = (ProductOption) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (optionPrice == null) {
            if (other.optionPrice != null)
                return false;
        } else if (!optionPrice.equals(other.optionPrice))
            return false;
        if (optionPriceForFamily == null) {
            if (other.optionPriceForFamily != null)
                return false;
        } else if (!optionPriceForFamily.equals(other.optionPriceForFamily))
            return false;
        if (optionPriceForNormal == null) {
            if (other.optionPriceForNormal != null)
                return false;
        } else if (!optionPriceForNormal.equals(other.optionPriceForNormal))
            return false;
        if (optionPriceForParty == null) {
            if (other.optionPriceForParty != null)
                return false;
        } else if (!optionPriceForParty.equals(other.optionPriceForParty))
            return false;
        if (optionPriceForSmall == null) {
            if (other.optionPriceForSmall != null)
                return false;
        } else if (!optionPriceForSmall.equals(other.optionPriceForSmall))
            return false;
        if (productOptionDescription == null) {
            if (other.productOptionDescription != null)
                return false;
        } else if (!productOptionDescription.equals(other.productOptionDescription))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "ProductOption [id=" + id + ", productOptionDescription=" + productOptionDescription + ", optionPrice="
                + optionPrice + ", optionPriceForSmall=" + optionPriceForSmall + ", optionPriceForNormal="
                + optionPriceForNormal + ", optionPriceForFamily=" + optionPriceForFamily + ", optionPriceForParty="
                + optionPriceForParty + "]";
    }



}

And this is how i am initialising the data.

@Component
public class WebsiteBootstrap implements ApplicationListener<ContextRefreshedEvent>{


    private ProductRepository productRepository;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent arg0) {
        initProducts();
    }

    public WebsiteBootstrap( ProductRepository productRepository,
            ProductOptionRepository productOptionRepository) {
        this.productRepository=productRepository;
    }



    private void initProducts() {
        ProductOption productOpton1=new ProductOption("mit Cocktailsauce", new BigDecimal(0), null, null, null, null);

        ProductOption productOpton2=new ProductOption("mit Joghurtsauce", new BigDecimal(0), null, null, null, null);
        ProductOption productOpton3=new ProductOption("ohne Sauce", new BigDecimal(0), null, null, null, null);


        Product product37= new Product("Falafel", ProductCategory.Vegatarische_Döner, "Wahl aus: mit Cocktailsauce, mit Joghurtsauce oder ohne Sauce.",  new BigDecimal(5.00));

        product37.getProductOptions().add(productOpton1);
        product37.getProductOptions().add(productOpton2);
        product37.getProductOptions().add(productOpton3);


            this.productRepository.save(product37);


    }

In database I am not able to find the product_id .

在此处输入图片说明

What am I missing in my mapping?

ProductOptions is in your case the child entity (it contains the column with foreign key), hence it's responsible for saving the relationship. You would have to save the entity on child side:

private void initProducts() {
    ProductOption productOpton1=new ProductOption("mit Cocktailsauce", new BigDecimal(0), null, null, null, null);
    ProductOption productOpton2=new ProductOption("mit Joghurtsauce", new BigDecimal(0), null, null, null, null);
    ProductOption productOpton3=new ProductOption("ohne Sauce", new BigDecimal(0), null, null, null, null);

    Product product37= new Product("Falafel", ProductCategory.Vegatarische_Döner, "Wahl aus: mit Cocktailsauce, mit Joghurtsauce oder ohne Sauce.",  new BigDecimal(5.00));

    product37.getProductOptions().add(productOpton1);
    product37.getProductOptions().add(productOpton2);
    product37.getProductOptions().add(productOpton3);

    productOpton1.setProduct(product37);
    productOpton2.setProduct(product37);
    productOpton3.setProduct(product37);

    this.productRepository.save(product37);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM