• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSF2.0 BeanValidation

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi together,

plan to edit my application an replace the default jsf validation implementation with a beanvalidation.
Read some articles and tutotials, all look very easy of course , so i re-implement one of my jsf-validatiors as BeanValidator

ValidatorInterface


Implementation of course:


Usage in my (hibernate) entity bean



Now the Problem.
I exeptected that the validation takes place the first time when jsf recives the formular commit and update the data model (at the same time the "normal" jsf validator makes his work),
but that seems a misunderstanding. The validator only becomes checked when hibernate try to persist the entity and at this position i get only a stacktrace because jsf doesn't seems to recognize
the "ConstraintViolationException" when it gets thrown by the backend.

Some ideas what i am doing/understanding wrong?

Thanks for helping
Dom
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It actually makes more sense to validate at commit time. It's probably less overhead than looking at each "set" invocation, and any validations that are enforced by schema constraints might not be detectable before then.

You can, of course, validate before updating the model. And JSF validators may be of some service as well. The exact degree of co-operation between different validators is in flux at the moment, so YMMV depending on the JSF implementation and JEE server you're using.

ORM services tend to prefer unchecked exceptions. So for best results, the business logic should surround your commit with a try/catch block and translate the validation exception to a FacesException or (and this is usually better) it should report an error. For example:
 
Dominik Müller
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi (after long time) again,

after some searching i found the problem with BeanValidation + JSF.
JSF does not support global class validation (ElementType.TYPE)

Have something found about the issue, that is seems, that is it because the lifecycle behaviour of JSF.
All values will be transferred seperatly from the post to the beans. So field-validation works fine, but
JSF doens't have any chance to validate the whole object.

Hibernate can do this of course when he tries to persist the object.

The solution to catch the exception doensn't work as listet. I have a typical architekt: ManagedBean -> ServiceClass (EJB) -> DAO (EJB) -> DatabaseCommit, but on ManagedBean side the only Exception i get is an:

javax.ejb.EJBException: Transaction aborted

somewhere in the stacktrace i can found the ValidationException... have you add a throws declaration to all your DAO-Methods, or how does it work in your case?

Thanks a lot
D
 
Alas, poor Yorick, he knew this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic