I have similar problem with validation of an object graph.
I have two JPA entities: A and B. B is referenced by A as oneToMany reference:
Code:
1 n
A --------> B
I installed EventListener for entity A with a @PrePersist method where I want to validate the entity.
Then I did the following:
Code:
EntityManager em;
A a = new A();
B b = new B();
b.setInvalidProperty();
a.addB(b);
em.merge(a);
I've debugged the DefaultMergeEventListener and observed, that at the runtime my @PrePersist callback method gets a copy of entity a (a_copy) without reference to b (the collection of B's in A was empty). Within the callback a_copy is then validated without referenced b entity.
The b entity is validated alone in the next call to @PrePersist callback method.
The ConstraintViolationException is then thrown but the RootBean and LeafBean are wrong: both the RootBean and the LeafBean are entity b.
Expected is: RootBean is a, LeafBean is b.
Is the object graph validation in @PrePersist callback possible at all?
Why the @PrePersist callback gets stripped entity without references?
If object graph validation in @PrePersist callback is not possible, are there any other suitable places in Hibernate stack to perform validation before a transient entity is persisted?