WItam tworzę sobie prosty system do pisania umów na na usługi telekomunikacyjne
chce zrobić tak. Stworzyć jakiś pakiet usług na początek chce powiązać dany pakiet z wieloma okresami długości umowy. Tak by na dany pakiet można było podpisać umowe tylko np na 12 miesięcy albo 15
tak wygląda mój produkt (pakiet)
@Entity
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "category_id")
private Category category;
@ManyToOne
@JoinColumn(name = "peri_id")
private Periods periods;
private BigDecimal price;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Periods getPeriods() {
return periods;
}
public void setPeriods(Periods periods) {
this.periods = periods;
}
}
tak wygląda klasa okres (admin może tworzyć nowe okresy)
@Entity
public class Periods {
@GeneratedValue(strategy=GenerationType.AUTO)
@Id
private Long id;
private String period;
@ManyToOne
private Product product;
@ManyToOne
private User userCreate;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getPeriod() {
return period;
}
public void setPeriod(String period) {
this.period = period;
}
public User getUserCreate() {
return userCreate;
}
public void setUserCreate(User userCreate) {
this.userCreate = userCreate;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
a tu mam dodatkową klasę by zamknąć to w jednej tabeli
@Entity(name = "product_periods")
public class ProductsPeriods {
@Id
@GeneratedValue
private Long id;
@OneToMany
private List<Periods> periodsList = new ArrayList<>();
@ManyToOne
@JoinColumn(name = "product_id")
private Product product;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public List<Periods> getPeriodsList() {
return periodsList;
}
public void setPeriodsList(List<Periods> periodsList) {
this.periodsList = periodsList;
}
}
muszę przyznać, że nie rozumiem tego za bardzo zapisać chce to w taki sposób
@PostMapping("/new/product")
public String saveProduct(Product product) {
productRepository.save(product);
periodsList.clear();
return "redirect:/system/dashboard/new/product";
}
jaki i też tak
@PostMapping("/new/product")
public String saveProduct(Product product) {
ProductsPeriods productPeriods = new ProductsPeriods();
productPeriods.setProduct(product);
productPeriods.setPeriodsList(periodsList);
productPeriodsRepository.save(productPeriods);
productRepository.save(product);
periodsList.clear();
return "redirect:/system/dashboard/new/product";
}
wszystkie okresy są zapisywane najpierw do do listy periodsList a następnie chce to wrzucić do bazy danych.
Jak to rozwiązać by było tak jak pisałem na początku ?