ShoppingList.java

  1. package ntnu.idatt2016.v233.SmartMat.entity.group;


  2. import com.fasterxml.jackson.annotation.JsonIgnore;
  3. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  4. import jakarta.persistence.*;
  5. import lombok.*;
  6. import ntnu.idatt2016.v233.SmartMat.entity.group.Group;
  7. import ntnu.idatt2016.v233.SmartMat.entity.product.Product;

  8. import java.util.ArrayList;
  9. import java.util.List;

  10. /**
  11.  * This class represents a shopping list
  12.  *
  13.  * @author Stian Lyng, Anders, Birk
  14.  * @version 1.3
  15.  */

  16. @NoArgsConstructor
  17. @AllArgsConstructor
  18. @Builder
  19. @Entity(name = "shopping_list")
  20. @Getter @Setter
  21. public class ShoppingList {
  22.     @Id
  23.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  24.     @Column(name = "shopping_list_id")
  25.     long shoppingListID;


  26.     @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  27.     @JoinColumn(name = "group_id")
  28.     @JsonIgnore
  29.     private Group group;

  30.     @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH},
  31.             fetch = FetchType.LAZY, mappedBy = "shoppingLists")
  32.     @JsonIgnoreProperties("shoppingLists")
  33.     private List<Product> products;


  34.     /**
  35.      * Adds a product to the shopping list
  36.      * @param product  the product to add to the shopping list
  37.      */
  38.     public void addProduct(Product product) {
  39.         if (products == null)
  40.             products = new ArrayList<>();
  41.         products.add(product);
  42.     }
  43. }