10 Replies Latest reply on Dec 13, 2006 1:47 PM by smokingapipe

    @In(create=true) throws exception when variable is null?

    smokingapipe

      I have a session bean that has a variable (instance member) like this:

      @In(create=true) Foo foo;

      Even if "foo" is null, that should work, because it will just create it, right? But it doesn't work; I have to do:

      @In(create=true,required=false) Foo foo;

      which doesn't make sense. It seems like if create is true, then required is superfluous, right? Or not right?

        • 1. Re: @In(create=true) throws exception when variable is null?
          pmuir

          Do you have a Seam component or factory called foo? Remember Seam does injection by variable name not class type.

          • 2. Re: @In(create=true) throws exception when variable is null?
            smokingapipe

            Actually, it's like this:

            @In(create=true) Foo createFoo;

            and I don't have an entity with a name of createFoo, and I don't have a factory called createFoo. I thought that @In(create=true) would just use the constructor for the class Foo? Do I also need a factory for it? Or should I use a method with the @Create annotation? I'm a bit lost, not knowing the nuances of where to put these things. I feel like I'm starting to get an understanding though.

            • 3. Re: @In(create=true) throws exception when variable is null?
              pmuir

              Injection is by variable name not by class. So

              @Name("foo")
              class Foo {
              ...
              }


              @In(create=true) Foo foo;
              
              // or
              
              @In(create=true, value="foo") Foo createFoo;


              • 4. Re: @In(create=true) throws exception when variable is null?
                smokingapipe

                I thought that if you had:

                @In(create=true) Foo createFoo


                that would look at the variable name, which is createFoo, and use that as the Seam variable name?

                Anyway, I tried:

                @In(create=true,value="createFoo") Foo createFoo


                and that still gave the same exception. The only time it doesn't give an exception is when I put in required=false, like:

                @In(create=true,required=false) Foo createFoo


                but then it looks like createFoo is never created. This makes no sense!

                Note that I don't have a "createFoo" role attached to the Foo class. Do I need that also?


                • 5. Re: @In(create=true) throws exception when variable is null?
                  smokingapipe

                  Ok, in the Foo class, I added:

                  @Roles({
                  @Role(name="createFoo", scope=ScopeType.CONVERSATION)
                  })


                  and that didn't help, so I'm at a loss as to how to do anything practical with @In(create=true), used together with @Out, like:

                  @In(create=true) @Out Foo createFoo;


                  It seems like it should just instantiate the thing and then outject it with the variable name of "createFoo", but that isn't happening and createFoo is null when the page renders. Huh? I must be doing something dumb here....


                  • 6. Re: @In(create=true) throws exception when variable is null?
                    gavin.king

                     

                    and I don't have an entity with a name of createFoo, and I don't have a factory called createFoo. I thought that @In(create=true) would just use the constructor for the class Foo?


                    Of course not! Not in JSF, not in Seam, not in any other web framework I have ever heard of.

                    • 7. Re: @In(create=true) throws exception when variable is null?
                      smokingapipe

                      Wait, so if create=true doesn't result in instantiating the variable, what does it do? I don't understand. From the Seam docs:

                      @In(create=true): Specifies that a component attribute is to be injected from a context variable at the beginning of each component invocation. If the context variable is null, an instance of the component is instantiated by Seam.



                      • 8. Re: @In(create=true) throws exception when variable is null?
                        gavin.king

                        It instantiates the Seam component associated with the context variable. Not just "any old class".

                        • 9. Re: @In(create=true) throws exception when variable is null?
                          smokingapipe

                          Ok, I figured it out. Foo wasn't "any old class", but there wasn't a "createFoo" role. To get Seam to instantiate something, you need to:

                          1. Make sure there is a Role with the necessary name. You can specify this with either @Name, or you can have additional component names by using @Role or @Roles.

                          2. In the session bean, you use @In(create=true,value="...") to tell Seam to instantiate that. I think the value="..." is optional if the variable name in the session bean is the same as the name on the entity (either the @Name or @Role).

                          I just did that and now it works.

                          I think I'm getting this thing under control. It seems like once I get fluent in this it will be an amazing way to do web development.

                          • 10. Re: @In(create=true) throws exception when variable is null?
                            smokingapipe

                            I just checked it, and as expected, if the role name is the same as the instance variable name in the session bean, value="...." is not necessary. I also put a @Valid annotation on it and everything is working.