9 Replies Latest reply on Jun 30, 2008 4:59 PM by schevus.swosborne.magellanhealth.com

    Hibernate Validators as Seam components?

      I want to inject some components for data access etc into my validators. Is it possible to use a hibernate validator class as a seam component? If not, is it possible to reach an instance of a seam component from the validator?

        • 1. Re: Hibernate Validators as Seam components?

          So far I've done this in components.xml




               <component name="myDao"
                    class="MyDao"
                    auto-create="true" scope="stateless" />
          
               <component name="myValidator"
                    class="MyValidator"
                    auto-create="true" scope="stateless">
                    <property name="myDao">#{myDao}</property>
               </component>



          myValidator is an instance of org.hibernate.Validator.Validator.
          myDao is a static property of myValidator, and I attempt to inject it via EL in components.xml.


          This doesn't work. When I reach the isValid-method of myValidator, myDao has value null.


          Does anyone have a better example of using seam-components from hibernate validator instances?

          • 2. Re: Hibernate Validators as Seam components?

            By the way, I cannot use seam annotations in myDao and myValidator since they are shared by a non-seam project.

            • 3. Re: Hibernate Validators as Seam components?
              gavin.king

              You could call Component.getInstance() from inside the validator class, I suppose...

              • 4. Re: Hibernate Validators as Seam components?

                I tried this (in the validators initialize()-method):


                this.myDao = (MyDao) Component.getInstance(MyDao.class);


                and got


                No @Name annotation for class: MyDao


                As listed previously, I already configured MyDao in components.xml - so it has no @Name annotation. Is this a bug? Is there another way to get hold of seam components from non-seam components?

                • 5. Re: Hibernate Validators as Seam components?
                  nickarls

                  Eirik Larsen wrote on Feb 20, 2008 08:47 AM:

                  Is there another way to get hold of seam components from non-seam components?


                  How does Contexts.lookupInStatefulContextorsomethinglikethat react?

                  • 6. Re: Hibernate Validators as Seam components?


                    How does Contexts.lookupInStatefulContextorsomethinglikethat react?

                    It works IF I already have myDao in a stateful scope. However, this is not so easy to accomplish in components.xml (I cannot annotate as MyDao is shared between projects).


                    I've created a temporary component that starts up myDao:


                    @Scope(APPLICATION)
                    @Name("daoHelper")
                    @Startup(depends = "myDao")
                    public class ValidatorHelper {}



                    I really hope to get rid of this one and do something similar to the @Startup-annotation in my components.xml instead. Ideas?

                    • 7. Re: Hibernate Validators as Seam components?
                      pmuir

                      Use Component.getInstance("myDao");

                      • 8. Re: Hibernate Validators as Seam components?

                        Use Component.getInstance("myDao")

                        Thanks Pete, that worked without daoHelper to initialize myDao.

                        • 9. Re: Hibernate Validators as Seam components?
                          schevus.swosborne.magellanhealth.com

                          Is there an easier way to do this if annotations are an option? Also, what are the repercussions of using Component.getInstance()? That is the method I am currently trying, but it is somehow causing an infinite loop leading to a StackOverflowException when I call flush(). Any help is appreciated.