3 Replies Latest reply on Dec 8, 2009 8:18 AM by ygardas

    Hibernate validations on seam components

    ygardas
      The problem I'm facing goes like this:

      There are two classes ClassOne and ClassTwo.

      @Name("classone")
      @Scope(ScopeType.CONVERSATION)
      @Entity
      @AutoCreate
      @Table(name = "classone", schema = "demo")
      @Inheritance(strategy = InheritanceType.JOINED)
      public class ClassOne implements Serializable {

        public ClassOne() {
          this.classTwo= new ClassTwo();
        }

        private ClassTwo classTwo;

        private String description;

        @Length(max = 1000, message = "{classone.description.length}")
        @Pattern(regex = "([a-zA-Z0-9\\s]*)", message = "{classone.description.alpha}")
        public String getDescription() {
          return description;
        }

        public void setDescription(String description) {
          this.description= description;
        }

      }


      @Name("classTwo")
      @Scope(ScopeType.CONVERSATION)
      @Entity
      @AutoCreate
      @Table(name = "classTwo", schema = "demo")
      public class ClassTwo {

        private String data;

        @Length(max = 7, message = "{classTwo.data.length}")
        @Pattern(regex = "([a-zA-Z0-9\\s]*)", message = "{classTwo.data.alpha}")
        public String getData() {
          return data;
        }

        public void setData(String data) {
          this.data = data;
        }

      }


      The validation messages for the 'description' is displayed on the view but for 'data' it is not displayed.

      Couldn't found out what exactly the reason might be!

      Please help me to solve this issue!
        • 1. Re: Hibernate validations on seam components
          ygardas

          Hi,


          I need help in solving this issue!


          Please suggest me some solution.

          • 2. Re: Hibernate validations on seam components

            Try adding @Valid to the classTwo property in ClassOne


            ClassOne.java


            @Name("classone")
            @Scope(ScopeType.CONVERSATION)
            @Entity
            @AutoCreate
            @Table(name = "classone", schema = "demo")
            @Inheritance(strategy = InheritanceType.JOINED)
            public class ClassOne implements Serializable {
              
              // snip
            
              @Valid // <-- tell Hibernate to additionally validate ClassTwo
              private ClassTwo classTwo;
            
              // snip
            
            }
            



            If that alone doesn't work, the only other thing I can think it could be is that you're missing @OneToOne on classTwo making Hibernate unaware that ClassOne and ClassTwo are related. You may need to add @OneToOne



            ClassOne.java


            @Name("classone")
            @Scope(ScopeType.CONVERSATION)
            @Entity
            @AutoCreate
            @Table(name = "classone", schema = "demo")
            @Inheritance(strategy = InheritanceType.JOINED)
            public class ClassOne implements Serializable {
            
              // snip
            
              @OneToOne // <-- tell Hibernate the two entities are related
              @Valid // <-- tell Hibernate to additionally validate ClassTwo
              private ClassTwo classTwo;
            
              // snip
            
            }
            



            Hope this helps!


            --mgbowman

            • 3. Re: Hibernate validations on seam components
              ygardas

              Hi,


              The annotations helped me!


              Thanks a lot!