1 2 Previous Next 19 Replies Latest reply on Dec 21, 2009 3:40 PM by Rui Santos

    Bypassing validations when using EntityQuery

    P Boevink Newbie

      This thread was started on the old forum....




      Posted: Sat Jan 19, 2008 19:08 PM    Post subject: Bypassing validations when using EntityQuery with example ob


        

      --------------------------------------------------------------------------------




      I'm using the EntityQuery framework class, and it's working fine. As per the seam docs I'm using the Example object to hold query parameters. It works very well.



      BUT, Seam is running the validations on the example object, validation fails, and the search can't proceed. While the entity validations are appropriate for persisting the entity (name must be at least 3 chars for instance), they don't apply to this use case.



      I'm not using s:validate or s:validateAll, which I thought was what triggered this behavior. Any suggestions? Is this how it is supposed to be?



      Thanks,



      Stu






      pete.muir@jboss.org



      Joined: Tue Jun 19, 2007 13:58 PM
      Posts: pete.muir@jboss.org
      Location: Edinburgh, Scotland
      Posted: Sun Jan 20, 2008 07:41 AM   


      Post subject: Re: Bypassing validations when using EntityQuery with exampl  


      --------------------------------------------------------------------------------



      No, its not. Validation is triggered in two places with Seam. (1) s:validate(All), (2) by hibernate when persisting an entity.



      Are you sure you don't have any validators being applied e.g. in s:decorate templates?




      I've exactly the same problem.
      The default display decorator is used as generated by seam-gen.
      A userLsit (entityQuery) is also generated by seam-gen.


      I would like to search for a user by e-mail address, but validation is performed (must be a well-formed email address) since there exists an @Email annotation on this attribute within the entity class.


      How to solve this?

        • 2. Re: Bypassing validations when using EntityQuery
          P Boevink Newbie

          Pete Muir wrote on Feb 18, 2008 04:52 PM:


          Show us code.


          I have an entity User.java:


          
          package web.entity.user;
          
          
          import .....
          
          import org.hibernate.validator.Email;
          
          
          
          @Entity
          
          public class User implements Serializable {
          
               
          
               //seam-gen attributes (you should probably edit these)
          
               private Long id;
          
               private Integer version;
          
               private String username;
          
               private String password;
          
               private String email;
          
              
          
               @Id @GeneratedValue
          
               public Long getId() {
          
                    return id;
          
               }
          
          
               public void setId(Long id) {
          
                    this.id = id;
          
               }
          
               
          
               @Version
          
               public Integer getVersion() {
          
                    return version;
          
               }
          
          
               private void setVersion(Integer version) {
          
                    this.version = version;
          
               }
          
          
               @Length(max=64)
          
               public String getUsername() {
          
                    return username;
          
               }
          
          
               public void setUsername(String username) {
          
                    this.username = username;
          
               }
          
          
               @Length(max=64)     
          
               public String getPassword() {
          
                    return password;
          
               }
          
          
               public void setPassword(String password) 
          
               {
          
                    this.password = password;     
          
               }
          
          
               @Email
          
               @Length(max=64)
          
               public String getEmail() {
          
                    return email;
          
               }
          
          
               public void setEmail(String email) {
          
                    this.email = email;
          
               }
          
          }
          
          
          



          My seam-gen generated entityQuery (UserList.java):


          package nl.wkm.orionglobe.web.session;
          
          
          import nl.wkm.orionglobe.web.entity.user.*;
          
          import org.jboss.seam.annotations.Name;
          
          import org.jboss.seam.framework.EntityQuery;
          
          import java.util.List;
          
          import java.util.Arrays;
          
          
          @Name("userList")
          
          public class UserList extends EntityQuery {
          
          
               private static final String[] RESTRICTIONS = {
          
                         "lower(user.email) like concat(lower(#{userList.user.email}),'%')",
          
                         "lower(user.password) like concat(lower(#{userList.user.password}),'%')",
          
                         "lower(user.username) like concat(lower(#{userList.user.username}),'%')", };
          
          
               private User user = new User();
          
          
               @Override
          
               public String getEjbql() {
          
                    return "select user from User user";
          
               }
          
          
               @Override
          
               public Integer getMaxResults() {
          
                    return 25;
          
               }
          
          
               public User getUser() {
          
                    return user;
          
               }
          
          
               @Override
          
               public List<String> getRestrictions() {
          
                    return Arrays.asList(RESTRICTIONS);
          
               }
          
          
          }
          
          




          My STRIPPED seam-gen generated UserList.xhtml:



          <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
          
                                       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          
          
          
          <html xmlns="http://www.w3.org/1999/xhtml"
          
                          xmlns:s="http://jboss.com/products/seam/taglib"
          
                          xmlns:ui="http://java.sun.com/jsf/facelets"
          
                          xmlns:f="http://java.sun.com/jsf/core"
          
                          xmlns:h="http://java.sun.com/jsf/html"
          
                          xmlns:a="http://richfaces.org/a4j"
          
                          xmlns:rich="http://richfaces.org/rich">
          
                                 
          
          
          <head></head>
          
          <body>    
          
              <h:messages globalOnly="true" styleClass="message" id="globalMessages"/>
          
              
          
              <h:form id="userSearch" styleClass="edit">
          
              
          
                  
          
                          <h:inputText id="username" value="#{userList.user.username}"/>
          
                   
          
                          <h:inputText id="email" value="#{userList.user.email}"/>
          
                                          
          
                  
          
                  <div class="actionButtons">
          
                      <h:commandButton id="search" value="Search" action="/system/UserList.xhtml"/>
          
                  </div>
          
                  
          
              </h:form>
          
           </body>
          
           </html>   
          
          



          The STRIPPED Seam-gen generated UserList.page.xml:



          <?xml version="1.0" encoding="UTF-8"?>
          
          <page xmlns="http://jboss.com/products/seam/pages"
          
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          
                xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.0.xsd">
          
                
          
               <param name="username" value="#{userList.user.username}"/>
          
               <param name="email" value="#{userList.user.email}"/>
          
          
          </page>
          
          




          All what's left now is a 'naked' html form with two input fields.
          If filling in a name in the second (e-mail address) field without @ and pressing the search button, a message will be shown: must be a well-formed email address


          I'm using:



          • jboss-seam-2.0.1.GA

          • jboss-4.2.2.GA

          • jdk1.5

          • 3. Re: Bypassing validations when using EntityQuery
            Siarhei Dudzin Apprentice

            Btw, I've hit the same problem yesterday. The code is generated by seam gen (of course validators were added at a later stage). It's very simple to test - just put a min length validator and try search with no search parms.

            • 4. Re: Bypassing validations when using EntityQuery
              P Boevink Newbie

              And did you manage to solve it?

              • 5. Re: Bypassing validations when using EntityQuery
                Siarhei Dudzin Apprentice

                No - I had to drop validdators, I'd say it's a good candidate for a JIRA.

                • 7. Re: Bypassing validations when using EntityQuery
                  P Boevink Newbie

                  Pete Muir wrote on Feb 19, 2008 05:54 PM:


                  This is due to the page parameters

                  JBSEAM-1587

                  Thanks!
                  Finally an answer...where can I vote for this issue ;-)

                  • 8. Re: Bypassing validations when using EntityQuery
                    marx3 Apprentice

                    As I understand, it's possible to fix it via page parameters, how?

                    • 10. Re: Bypassing validations when using EntityQuery
                      daniel varga Newbie

                      You can use a HashMap instance as your example component, i.e.:


                         


                      <component name="exampleSubsystem" class="java.util.HashMap" scope="conversation" />
                              
                      <framework:entity-query name="subsystemList" ejbql="select s from CaasSubsystem s" max-results="10"      scope="conversation" 
                              order="subsystem" >          
                              <framework:restrictions>
                                      <value>lower(subsystem) like lower(#{exampleSubsystem.subsystem})</value>
                                      <value>lower(displayName) like lower(#{exampleSubsystem.displayName})</value>
                              </framework:restrictions>
                      </framework:entity-query>
                      


                      • 11. Re: Bypassing validations when using EntityQuery
                        Pablo Candiani Newbie

                        Hi, you can create a field @Transient (i.e.:mailtransient) in User.java


                        Then, in UserList.java add a restriction on User.email with this field (UserList.user.mailtransient),


                        add in userList.page.xml a parameter mailtransient, and modify UserList.xhtml the search field email x mailtransient.


                        (It's a posibility, but is not very ellegant, but should work)

                        • 13. Re: Bypassing validations when using EntityQuery
                          Ingo Jobling Master

                          Just replace the example bean with a Hashmap, as suggested by the previous poster. 


                          Why does Seam Gen generate code that is so obviously wrong?

                          • 14. Re: Bypassing validations when using EntityQuery
                            Dan Allen Master

                            It's not a problem in seam-gen. The issue is that page parameters are applying the validators unconditionally. seam-gen just happens to use page parameters and expects them to do the right thing. So let's fix the real problem.

                            1 2 Previous Next