【问题标题】:HIbernate update with null on onetomany relation单对多关系上的 HIbernate 更新为 null
【发布时间】:2013-08-12 07:20:46
【问题描述】:

我有以下表格:

  • 产品(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 注释)?

【问题讨论】:

  • 异常的堆栈跟踪是什么,导致它被抛出的代码是什么? ProductProperties 实体是如何定义的?
  • 您已启用孤立删除。请参考stackoverflow.com/questions/306144/…
  • @DevBlanked 感谢您的回答,“orphanRemoval=true”确实可以解决问题 :)

标签: java spring hibernate exception one-to-many


【解决方案1】:

答案由 cmets 中的 Dev Blanked 给出。 这是解决方案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 2015-10-24
    • 2022-10-07
    • 1970-01-01
    相关资源
    最近更新 更多