-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Validator + Spring @Transactional problem
PostPosted: Tue Apr 12, 2011 2:00 am 
Newbie

Joined: Sun Feb 14, 2010 10:13 am
Posts: 17
Hello

I do the following operation with Spring 3.0 + Hibernate Validator 4.1.0:

Code:
@Transactional
public void updateEntity(MyEntity e) {
   this._hibernateTemplate.saveOrUpdate(e); // supposed to validate e and save or update it
   doSomeCalculations(e); // changes the validation terms
   this._hibernateTemplate.saveOrUpdate(e.getAssociation());
}


Now here's the weird part: when e is transient, the saveOrUpdate operation validates e right away (fires a PreUpdateEvent) before creating it and not waiting for the transaction to commit. But when e is detached (thus updated to DB), Hibernate Validator is not called by PreUpdateEvent until the transaction ends, and thus the validation fails because e is no longer valid after doSomeCalculations(e)

This behavior is inconsistent and poses a problem for me. Why is Hibernate validating e immediately when it created but waiting for the transaction to commit when it is only persisted (updated)?

I need it to validate e right away, not when committing, whether creating or updating it..

- - -
Posted this also at http://forum.springsource.org/showthread.php?t=107434 because i'm not sure where is the problem.


Top
 Profile  
 
 Post subject: Re: Validator + Spring @Transactional problem
PostPosted: Tue Apr 12, 2011 5:13 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

if you want to control validation yourself it might be better to manually control the validation by using the Validator directly. I am not sure why exactly the behavior is as described, but it is in accordance w/ the spec. Here are some quotes from the persistence spec:

Quote:
This specification supports the use of bean validation for the automatic validation of entities upon the
pre-persist, pre-update, and pre-remove lifecycle validation events.


Quote:
The PreUpdate and PostUpdate callbacks occur before and after the database update operations to
entity data respectively. These database operations may occur at the time the entity state is updated or
they may occur at the time state is flushed to the database (which may be at the end of the transaction).

Note that it is implementation-dependent as to whether PreUpdate and PostUpdate callbacks
occur when an entity is persisted and subsequently modified in a single transaction or
when an entity is modified and subsequently removed within a single transaction. Portable
applications should not rely on such behavior.


You can turn off automatic validation via setting javax.persistence.validation.mode to none. Or you can define which validation group should be targeted via the events using javax.persistence.validation.group.pre-[persist|update|remove]. Not sure whether this helps you, since it sill does not give any guarantee when the event is triggered.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validator + Spring @Transactional problem
PostPosted: Wed Apr 13, 2011 12:23 pm 
Newbie

Joined: Sun Feb 14, 2010 10:13 am
Posts: 17
Thank you Hardy

Can you refer me to some source that explains how to manually validate the entity?
The actual problem is that the entity is validated only at the end of the transaction, so even if I validate manually during the transaction, will it not be automatically validated again when committing?

Other solutions are quite complex, considering that otherwise entities validation works pretty well. If I turn off auto validation (suppose I create a special group for this entity) won't it mean I have to manually validate it on every place it's updated?

I do think that Hibernate Validator should be improved to provide some guarantee as for validation time. It's not uncommon to change entities during a transaction, so this behavior is expected to be consistent (or at least controllable).

--Yuval


Top
 Profile  
 
 Post subject: Re: Validator + Spring @Transactional problem
PostPosted: Wed Apr 13, 2011 2:41 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

First of all I would turn off event based validation by setting javax.persistence.validation.mode to none. Then I would get hold of the ValidatorFactory and create my Validator directly from there. How you do that will depend on your framework. Given that you are using Spring I would assume that you can just get the factory of validator itself injected into your class.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validator + Spring @Transactional problem
PostPosted: Wed Apr 13, 2011 5:30 pm 
Newbie

Joined: Sun Feb 14, 2010 10:13 am
Posts: 17
Thanks, I can try that.

I had another idea: What if I persist / update e inside a nested transaction? If I understand the concept correctly that should force Hibernate Validator to validate e when committing the nested transaction at the latest, but still rollback if an exception occurs afterward in the parent transaction. Can this work? (I use Hibernate 3.6)

Yuval


Top
 Profile  
 
 Post subject: Re: Validator + Spring @Transactional problem
PostPosted: Thu Apr 14, 2011 4:12 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Afaik there is no nested transaction support in Hibernate. So I am not sure what you have in mind.

--Hardy


Top
 Profile  
 
 Post subject: Re: Validator + Spring @Transactional problem
PostPosted: Thu Apr 14, 2011 5:59 am 
Newbie

Joined: Sun Feb 14, 2010 10:13 am
Posts: 17
Yeah, I started exploring that and quickly learned that there is no such support.

But I did find another simple 1-liner solution! Apparently flushing the session causes Hibernate Validator to validate the updated entity.
So I can just flush the session right after the update to e, without committing of course, and then get the validation on time.

Thank you for your help,
Yuval


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.