Вот моя структура базы данных:
Когда я делаю REST-вызов user_product с намерением обновить значение продукта, которое я продолжаю получать исключение с нулевым указанием Я предполагаю, что проблема заключается в ServiceImpl, хотя независимо от того, что я изменяю, ошибка по-прежнему состоит.
Мой serviceImpl:
public void update(UserProductVO userProductVO) {
UserProduct userProduct = new UserProduct();
userProduct.setId(new UserProductId(userProductVO.getProduct().getId(), userProductVO.getUser().getId()));
userProduct.setUser(userProductVO.getUser());
userProduct.setProduct(userProductVO.getProduct());
UpdatedProduct updatedProduct = new UpdatedProduct(userProductVO.getAmountOfNewProducts());
updatedProductRepository.save(updatedProduct);
userProduct.getUpdatedProducts().add(updatedProduct);
userProductRepository.save(userProduct);
Product product = productRepository.getById(userProductVO.getProduct().getId());
product.setAmount(product.getAmount() + userProductVO.getAmountOfNewProducts());
productRepository.save(product);
}
В журнале ошибок указано, что проблема здесь:
userProduct.getUpdatedProducts().add(updatedProduct)
Буду признателен за любую подсказку, где я могу испортить. Заранее спасибо.
Редактировать:
Мой класс UserProduct:
@Entity
@Table(name = "user_product")
public class UserProduct {
@EmbeddedId
private UserProductId id;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("userId")
@JsonBackReference(value = "userJ")
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("productId")
@JsonBackReference(value = "productJ")
private Product product;
@OneToMany(
mappedBy = "userProduct",
cascade = CascadeType.ALL,
orphanRemoval = true
)
@JsonManagedReference(value = "userProductJ")
private List<UpdatedProduct> updatedProducts;
public UserProduct() {
}
public UserProduct(UserProduct user, UserProduct product, int amountOfNewProducts) {
}
public UserProduct(User user, Product product, List<UpdatedProduct> updatedProducts) {
this.user = user;
this.product = product;
this.updatedProducts = updatedProducts;
}
..getters/setters/hashcode/equals
Редактировать2:
Мой класс обновленного продукта:
@Entity
@Table(name = "updated_product")
public class UpdatedProduct {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "amount_of_new_products")
private int amountOfNewProducts;
@Column(name = "updated_on")
private Date updatedOn = new Date();
@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference(value = "userProductJ")
private UserProduct userProduct;
public UpdatedProduct() {
}
public UpdatedProduct(int amountOfNewProducts) {
this.amountOfNewProducts = amountOfNewProducts;
}