2

I have the following tables :

  • PRODUCT (id_product, name)
  • PROPERTY (id_property, name)
  • PRODUCT_PROPERTIES (id_product, id_property)

(all the fields are not nullable)

And the following hibernate mapping :

class Product {
    @Id
    private Integer id;

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

    (...)    
}

When I update the product class by adding or removing rows from the "productProperties" field, the rows are updated correctly in the PRODUCT_PROPERTIES table.

The problem is that when "productProperties" is null or empty, Hibernate throws a ConstraintViolationException.

Since I need the "productProperties" to be empty sometimes, is there a proper solution to this problem (something like @ZeroToMany annotation) ?

Christos Loupassakis
  • 1,112
  • 3
  • 15
  • 23

1 Answers1

1

The answer is given by Dev Blanked in the comments. This is the solution :

@OneToMany(mappedBy="foo", orphanRemoval=true)
Christos Loupassakis
  • 1,112
  • 3
  • 15
  • 23