У меня есть две сущности Product и Product Options. Продукт может иметь несколько вариантов продукта, и, как видно, я сопоставил его с отношением oneToMany.
@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;
}
}
И 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 + "]";
}
}
И вот как я инициализирую данные.
@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);
}
В базе данных я не могу найти product_id
.
Чего мне не хватает в моем отображении