java - HIbernate 更新与 null ononetomany 关系

标签 java spring hibernate exception one-to-many

我有以下表格:

  • 产品(id_product,名称)
  • 属性(id_property,名称)
  • PRODUCT_PROPERTIES(id_product、id_property)

(所有字段都不能为空)

以及以下 hibernate 映射:

class Product {
    @Id
    private Integer id;

    @OneToMany(mappedBy="product")
    @Cascade({CascadeType.ALL})
    private Set<ProductProperties> productProperties = new HashSet<ProductProperties)(0);

    (...)    
}

当我通过在“productProperties”字段中添加或删除行来更新产品类别时,这些行在 PRODUCT_PROPERTIES 表中正确更新。

问题是当“productProperties”为 null 或空时,Hibernate 会抛出 ConstraintViolationException。

由于有时我需要“productProperties”为空,是否有解决此问题的适当方法(类似于@ZeroToMany 注释)?

最佳答案

答案由 Dev Blanked 在评论中给出。 这是解决方案:

@OneToMany(mappedBy="foo", orphanRemoval=true)

关于java - HIbernate 更新与 null ononetomany 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18162282/

相关文章:

java - 使用还是不使用DTO?

java - 重构大型 JSON 多次调用

java - 用户定义对象的 ArrayList 的 indexOf() 不起作用

java - Eclipse 项目随处停止

java - Spring 事务上下文包装?

java - 当我们在 hibernate 中删除实体时会检查版本吗?

java - JPA 存储库方法 List<Integer> 返回结果为 List<BigDecimal> 为什么?

java - hibernate 公式的 JPA 替代品

java - 严重 : Failed to initialize Jenkins

java - 如何在Spring Hibernate中实现长时间运行的进程?