5 Replies Latest reply on Jul 29, 2008 12:05 PM by digdas.seam.digdas.nl

    Don't merge+flush when Complex Validation in Action Method fails

    swenbarth
      I want to achieve the following:

      in an action method bound to a form-submit, I want to perform some complex validations.
      When those validations fail, I want to suppress the auto-merge/flush at the end of the action method.


      Is this somehow possible?


      I DON'T want to use flushmode=MANUAL, because I do want to have the auto-commit behavior in most cases.

      I DON'T want to setRollbackOnly on the transaction, as this destroys the PersistenceContext.

      I DON'T want to throw an exception, as this causes the error page to be displayed, and not my form page to be re-displayed.

      I DON'T want to define some @AssertTrue methods on my Entity, because this leads to a rollback when the validation fails.

      I DON'T want to define some validator method on a hidden input and extract all input values from the component tree, as this is cumbersome.
        • 1. Re: Don't merge+flush when Complex Validation in Action Method fails
          swenbarth
          Currently, I'm using the solution with flushmode = MANUAL, which after my tests I feel is the  most elegant one;
          but it would be nice to be able to use the standard flushmode instead,
          and just somehow tell the EntityManager "ok, the validation failed, so don't do anything after returning from this method".
          • 2. Re: Don't merge+flush when Complex Validation in Action Method fails
            stephen
            Oh, how I wished that there would be a clean solution for this.

            I think the best (even if cumbersome) way to do this is the last topic in your "DON't want" list:
            "validator method on a hidden input and extract all input values from the component tree". That way the local component values never would make it into the entity model.

            We should really blame JSF for this. There simply should be some dedicated way to have multi-component/form-level validations.

            I recently had to correct a bug in production that occurred because some developer not that accustomed to Seam and hibernate added validation code to an action handler and was not aware of automatic flushing. (My dirty fix was to use setRollbackOnly, because no long running conversation was active anyway.)

            There is another (cumbersome) solution that is missing in you list:
            Over the time I found that dedicated value objects should not be abandoned completely, but do sometimes make life easier:
            - When the GUI design differs sufficiently from the entity model, then they are the way to go anyway.
            - They avoid issues like the one you are describing.
            - Developers without much experience in hibernate and seam do not need to wrap their minds around the intricate (even though sensible) way a persistence context, long and short conversations, and transactions interact.

            A little more work to copy data, less bugs. Every decent IDE will help you create DTOs quickly.


            BTW: AFAICS there is no automatic "merge" as you thread title suggests.
            • 3. Re: Don't merge+flush when Complex Validation in Action Method fails
              javacoryd

              There are a few different solutions to this:


              1) Back your pages with entity beans, use MANUAL flush mode with a conversation, flush() when you want.


              2) Back your pages with DTOs.  Apply the changes to the entities from the DTOs when you want.


              3) Back your pages with entity beans, but after you find the entities, evict them from the entity manager.  Merge them when you want to update.  This is fraught with danger if you have lazy relationships.


              4) Don't use the SeamPersistenceContext ( @In entityManager ) and control the transactions yourself using the EJB3 @PersistenceContext.


              We have a fairly large SEAM application and have struggled with this, but  solution 1 has worked well for us.


              Cory.


              • 4. Re: Don't merge+flush when Complex Validation in Action Method fails
                djn

                Our SEAM app is also of significant size, and Cory's solution 1 also works for us.


                However, according to the documentation (section 9.3.3 of the seam reference for 2.0.2.sp1) only hibernate supports MANUAL flushmode, so if you use other JPA providers, you are out of luck.

                • 5. Re: Don't merge+flush when Complex Validation in Action Method fails
                  digdas.seam.digdas.nl

                  Is the following not what you need?


                  JBoss documentation


                  Search for
                  @Validator
                  in chapter: 29.2. Annotations


                  Make your home-object as input?