【问题标题】:Why am i getting a NullPointerException when trying to save an entity to a list?为什么在尝试将实体保存到列表时出现 NullPointerException?
【发布时间】:2020-06-29 13:48:15
【问题描述】:

这是我的数据库结构:

当我对 user_product 进行 REST 调用以更新产品值时,我不断收到指向空值的异常。我的假设是问题出在 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;
    }

【问题讨论】:

  • 你能粘贴UserProduct类吗?
  • 刚刚做了,希望对你有帮助。

标签: java mysql sql jpa spring-data


【解决方案1】:

您必须使用空列表初始化 UserProduct 类,而不是 null:

public class UserProduct {

    private List<UpdatedProduct> updatedProducts = new ArrayList<>();

    // rest of the fields

}

为简洁起见,省略了所有注释。

【讨论】:

  • 它解决了这个问题,虽然现在我得到一个空列表作为响应。我不是在写信息吗?
  • 我需要UpdatedProduct类来告诉;>
  • @aratata 没问题;> 我认为您需要先输入updatedProduct.setUserProduct(userProduct);,然后再保存到userProductRepository.save(userProduct);
  • 成功了!虽然我可以再问一个问题吗?很抱歉,虽然我很少找到愿意提供帮助的人。
  • 这是因为您每次都使用new 创建UserProduct。如果要更新集合,则必须首先在数据库中查找此类 UserProduct 是否存在以及何时不存在 - 创建一个
猜你喜欢
  • 1970-01-01
  • 2013-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
  • 2014-09-05
  • 1970-01-01
  • 2021-11-30
相关资源
最近更新 更多