Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: @Validate and JPA Life Cycle and DefaultMergeEventListener
PostPosted: Tue Oct 19, 2010 12:51 pm 
Newbie

Joined: Tue Oct 19, 2010 11:16 am
Posts: 5
I want to validate detached entities after a merge via a ValidationListener in two steps. On @PrePersist the Constraints in the Group PrePersist.class and on @PostPersist the the Constraints in the Group PostPersist.class.

On OneToMany relations I want to Validate the associated beans with @Valid.

But in the @PrePersist Event in my ValidationListener the Root Bean has an empty Collection. And the method in @PrePersist will be called 2 times. Once with the Root Bean and once with the associated bean that should be in the @OneToMany collection. I expect that it will be called once with the root bean and that I get a Constraint like that:

RootBean : BeanA
LeafBean : BeanB
PropertyPath : beanBs[0].anyWrongValue

Waht I found out is that the copyValues Method in DefaultMergeEventListener gets an Object entity (Root Bean) with @OneToMany-collection size() == 1 and in the Object target the Root Bean has an empty @OneToMany collection.

What can I do?

The Problem is that in the ConstraintViolation the RootBean and the LeafBean is the same!

I use hibernate-core 3.6.0.Final !!! and hibernate validator 4.1

The associations in the Entities looks like that:

Code:
public class BeanA ... { ...
@OneToMany(cascade = CascadeType.ALL)
@NotNull
@Valid
private List<BeanB> beanBs = new ArrayList<BeanB>();
...}

public class BeanB ... { ...
@ManyToOne(optional = true)
@JoinColumn(name = "B_ID")
@NotNull
private BeanA beanA ;
...}


The ValidationListener looks like that:
Code:
...
@PrePersist
public void onPreChange(T object) { ....
   Set<ConstraintViolation<T>> constraintViolations = validator.validate(object, PrePersist.class);
...}...


Last edited by bennixview on Thu Oct 21, 2010 8:23 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: @Validate and JPA Life Cycle and DefaultMergeEventListener
PostPosted: Thu Oct 21, 2010 6:31 am 
Newbie

Joined: Thu Oct 21, 2010 5:48 am
Posts: 9
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?

_________________
М.стер Ха..ерн..т


Top
 Profile  
 
 Post subject: Re: @Validate and JPA Life Cycle and DefaultMergeEventListener
PostPosted: Thu Oct 21, 2010 8:22 am 
Newbie

Joined: Tue Oct 19, 2010 11:16 am
Posts: 5
Hi wrungel,

did you tried also the latest version of hibernat core 3.6. I update to it becaus of TypeFactory.replace was deprected in 3.5.6 and i hoped that the newer version fixed the problem. For details see : http://opensource.atlassian.com/project ... e/HHH-5138

But with version 3.6 i also get the same problem.


Last edited by bennixview on Tue Oct 26, 2010 5:13 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: @Validate and JPA Life Cycle and DefaultMergeEventListener
PostPosted: Thu Oct 21, 2010 8:30 am 
Newbie

Joined: Thu Oct 21, 2010 5:48 am
Posts: 9
bennixview wrote:
did you tried also the newest version of hibernat core 3.6. I update to it becaus of TypeFactory.replace was deprected in 3.5.6 and i hoped that the newer version fixed the problem. For details see : http://opensource.atlassian.com/project ... e/HHH-5138
But with version 3.6 i also get the same problem.

I tried it with version 3.6 only.

_________________
М.стер Ха..ерн..т


Top
 Profile  
 
 Post subject: Re: @Validate and JPA Life Cycle and DefaultMergeEventListener
PostPosted: Tue Oct 26, 2010 5:17 am 
Newbie

Joined: Tue Oct 19, 2010 11:16 am
Posts: 5
It looks as if the problem is already known.

http://opensource.atlassian.com/project ... e/HHH-1914

What can I do now ... ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.