I have encountered a strange phenomenon. In a reduced form my entity classes look like this:
@Entity
@Audited
public class Product {
@Id
@Column(length = 32)
private String id = IdGenerator.createId();
/** Descriptive texts, samples, comments etc.; ONIX composite Product/OtherText */
@OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="product_id")
@Index(name="idx_prod_text")
private Set<Text> texts;
...
}
@Entity
@Audited
public class Text {
@Id
@Column(length = 32)
private String id = IdGenerator.createId();
@NotNull
@Column(nullable=false, length=3)
private String type;
...
}
HashCode and equals work on the id field. I have built a generic pre-validation routine for auto correction and generation of warnings in a user friendly way. Auto correction for Texts with a null type is removal of this text from the collection in the product ( by calling product.getTexts().removeAll(entitiesToRemove)
).
Now I got a product (previously read from DB, so attached to Hibernate session), which gets appended a Text with type null. It is auto corrected, and the Text in question is removed from the collection as described above. So far, so good; inspection of the Product in the debugger shows that the Text is not in the texts collection anymore. But when I call productRepository.saveAndFlush(product)
(), I get a
javax.validation.ConstraintViolationException: Validation failed for classes [de.vlx.metadatastore.model.product.Text] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='darf nicht null sein', propertyPath=type, rootBeanClass=class de.vlx.metadatastore.model.product.Text, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]
So it seems that this text is still validated by Hibernate? Even if it never was in the data base (which also has of course a NotNull constraint), and is not in the texts collection of the Product anymore?
Even stranger: when I tried to hotfix this by setting a dummy value on the type of the removed entity, I don't get the above exception, but a org.springframework.dao.DataIntegrityViolationException
complaining about the fact that type is null.
Which kind of magic is at work, and what can I do to fix that?
Used versions: Hibernate 4.1.7.Final Spring-data-jpa 1.6.0.RELEASE Spring-core 3.2.9.RELEASE
Aucun commentaire:
Enregistrer un commentaire