8 Replies Latest reply on Oct 5, 2007 2:02 AM by Luiz Fernando Signorelli Gonçalves

    Problem with injection @In

    Luiz Fernando Signorelli Gonçalves Newbie

      Hi everyone,

      I have a code like this:

      
      @Name("entity")
      public class Entity{
      
      ...
      
      }
      


      and

      
      @Name("myPOJO")
      public class MyPOJO{
      
      @In
      private Entity entity;
      
      ...
      }
      
      
      



      When I load the page that access this bean i get the error: In attribute requires non-null value: MyPOJO.entity

      Than I changed my POJO to:

      
      @Name("myPOJO")
      public class MyPOJO{
      
      @In(create=true)
      private Entity entity;
      
      ...
      }
      


      And now it works!
      But in the "hibernate" example that comes with Seam distribution the fist code works normaly.

      Anyone can explain this?

      Tanks

        • 1. Re: Problem with injection @In
          Matt Drees Master

          Which part of the hibernate example are you referring to?

          • 2. Re: Problem with injection @In
            Luiz Fernando Signorelli Gonçalves Newbie

             

            "matt.drees" wrote:
            Which part of the hibernate example are you referring to?


            This one:

            @Entity
            @Name("user")
            @Scope(SESSION)
            @Table(name="Users")
            public class User implements Serializable
            {
             private String username;
             private String password;
             private String name;
            
             public User(String name, String password, String username)
             {
             this.name = name;
             this.password = password;
             this.username = username;
             }
            
             public User() {}
            
             @NotNull
             @Length(max=100)
             public String getName()
             {
             return name;
             }
             public void setName(String name)
             {
             this.name = name;
             }
            
             @NotNull
             @Length(min=5, max=15)
             public String getPassword()
             {
             return password;
             }
             public void setPassword(String password)
             {
             this.password = password;
             }
            
             @Id
             @Length(min=5, max=15)
             public String getUsername()
             {
             return username;
             }
             public void setUsername(String username)
             {
             this.username = username;
             }
            
             @Override
             public String toString()
             {
             return "User(" + username + ")";
             }
            }
            


            The POJO:

            @Scope(SESSION)
            @Name("bookingList")
            public class BookingListAction implements Serializable
            {
             @In
             private Session bookingDatabase;
            
             @In
             private User user;
            
             @DataModel
             private List<Booking> bookings;
            
             @DataModelSelection
             private Booking booking;
            
             @Factory("bookings")
             public void find()
             {
             bookings = bookingDatabase.createQuery("from Booking b where b.user = :user order by b.checkinDate")
             .setParameter("user", user)
             .list();
             }
            
             public String cancel()
             {
             Booking cancelled = (Booking) bookingDatabase.get(Booking.class, booking.getId());
             if (cancelled!=null) bookingDatabase.delete( cancelled );
             refresh();
             return "cancelled";
             }
            
             public void refresh()
             {
             booking = null;
             find();
             }
            
            }


            Another one:
            @Scope(EVENT)
            @Name("register")
            public class RegisterAction
            {
            
             @In
             private User user;
            
             @In
             private Session bookingDatabase;
            
             @In
             private FacesMessages facesMessages;
            
             private String verify;
            
             public String register()
             {
             if ( user.getPassword().equals(verify) )
             {
             List existing = bookingDatabase.createQuery("select username from User where username=:username")
             .setParameter("username", user.getUsername())
             .list();
             if (existing.size()==0)
             {
             bookingDatabase.persist(user);
             return "login";
             }
             else
             {
             facesMessages.add("username already exists");
             return null;
             }
             }
             else
             {
             facesMessages.add("re-enter your password");
             verify=null;
             return null;
             }
             }
            
             public String getVerify()
             {
             return verify;
             }
            
             public void setVerify(String verify)
             {
             this.verify = verify;
             }
            
            }
            



            I tried to put my components in the SESSION scope, lilke in the example, but still don't work.

            Am I missing some configuration?

            • 3. Re: Problem with injection @In
              Saeed Iqbal Novice

              exactly what was causing me alot of waste of time and frustration. Something that works in the examples should also work in our projects. whats up with that?

              • 4. Re: Problem with injection @In
                Stuart Douglas Master

                Are you referring to the booking example?

                The reason why it works in the example is because of this little bit of code in the AuthenticatorBean:

                @Out(required=false, scope = SESSION)
                private User user;

                when a user logs in the user entity is outjected to session scope, allowing it be used by other beans. When you think about it it makes sense that an exception is thrown, how can you inject the current user if they have not logged in yet?

                @In without create=true is basically saying that you want an initialized User injected, with create=true you are saying that you do not care if it has been initialized or not, you just want a user.

                • 5. Re: Problem with injection @In
                  Nicklas Karlsson Master

                  Well, as I've understood it, Seam creates non-existing components as they are referenced. So if you inject A into B it might work fine if you have referenced A earlier in the page.

                  I don't think session scope is any different, if you only reference it late in the page it doesn't exist before that.

                  • 6. Re: Problem with injection @In
                    Luiz Fernando Signorelli Gonçalves Newbie

                     

                    "saeediqbal1" wrote:
                    exactly what was causing me alot of waste of time and frustration. Something that works in the examples should also work in our projects. whats up with that?


                    What a I don't undestand is that I can run the example perfetly, and I based my projetc in this example.

                    Hope someone here can see what I couldn't.

                    • 7. Re: Problem with injection @In
                      Luiz Fernando Signorelli Gonçalves Newbie

                       

                      "swd847" wrote:
                      Are you referring to the booking example?

                      The reason why it works in the example is because of this little bit of code in the AuthenticatorBean:

                      @Out(required=false, scope = SESSION)
                      private User user;

                      when a user logs in the user entity is outjected to session scope, allowing it be used by other beans. When you think about it it makes sense that an exception is thrown, how can you inject the current user if they have not logged in yet?

                      @In without create=true is basically saying that you want an initialized User injected, with create=true you are saying that you do not care if it has been initialized or not, you just want a user.


                      Ok. It makes sense now...

                      Tanks for the explanation.



                      • 8. Re: Problem with injection @In
                        Luiz Fernando Signorelli Gonçalves Newbie

                         

                        "swd847" wrote:

                        when a user logs in the user entity is outjected to session scope, allowing it be used by other beans. When you think about it it makes sense that an exception is thrown, how can you inject the current user if they have not logged in yet?



                        Yes, I tought i had understood but....

                        Why an exception is not thrown in the register page if you didn't passed by the Authenticator yet?

                        I'm confused.