3 Replies Latest reply on Sep 6, 2010 9:44 AM by nimo22

    constructor not called of embeeded objects

    nimo22

      I have a no-scoped (entity) bean:


      public class User implements Serializable{ 
      ...
      
      private Role role;
      // getter/setter
      
      }



      I inject a new instance of User into my bean:


      @SessionScoped @Named("bean")
      public class MyBean implements Serializable{ 
      
      @Inject Users findUser;
      
      }




      when referring in jsf,
      then it throws a java.lang.NullPointerException,
      because the property role of instance user is null.



      <h:panelGrid columns="2">
      <h:outputText value="ID-User" />
      <h:inputText value="#{bean.findUser.id}" converter="javax.faces.Integer" />
      
      <h:outputText value="ID-Role" />
      <h:inputText value="#{bean.findUser.role.id}" converter="javax.faces.Integer"/>
      </h:panelGrid>




      So I have to use something like this in my code:


      @SessionScoped @Named("bean")
      public class MyBean implements Serializable{ 
      
      @Inject Users findUser;
      
      @PostConstruct
      public void initialize()
      {
      // instantiate a new instance of role to avoid the NPE
      findUsers.setRole(new Role());
      }
      
      }
      



      Why does CDI not instantiate a new instance automatically?
      Of course, a new instance of Users is injected,
      but Users has objects within, which should also get a new instance (when needed).


        • 1. Re: constructor not called of embeeded objects
          pmuir

          Do you @Inject Role into Users?

          • 2. Re: constructor not called of embeeded objects
            nimo22

            No, I did not @Inject Role into Users.


            That was my fault.
            I have thought, weld creates (inject) new instances of objects automatically.



            So I have to use this code (both versions work):



            version 1:


            public class User implements Serializable{ 
            ...
            
            private @Inject Role role;
            // getter/setter
            
            }




            version 2:



            public class User implements Serializable{ 
            ...
            
            private Role role = new Role();
            // getter/setter
            
            }




            I guess, I will use version 2 as these objects are pojos (entitybeans).

            • 3. Re: constructor not called of embeeded objects
              nimo22

              Another thing is:



              There are cases, I do need a new Role-Object when instantiating a new User-Object.
              But there are also cases, I do not need a new Role-Object  - so (for a big project), it is a waste to create a new Instance without needing it. I can use a NullObjectPattern, but this is annoying.


              Should I do it that way:




              public class User implements Serializable{ 
              ...
              
              // this is actually null,
              // I should avoid using Role role = new Role() or @Inject Role role
              // because it will be a waste, if I do not need it.
              private Role role;
              // getter/setter
              
              }
              
              public class MyBean implements Serializable{ 
              ...
              
              @Inject User u;
              
              @PostConstruct
              public void init()
              {
              
              // this is a little annoying:
              // I can eliminate that boilerplate-code by creating a new instance of role 
              // within a user-class, but this would be a waste if I do not need a role-instance
              u.setRole(new Role());
              }
              
              }
              





              So, how can I solve that in a pretty way?
              When using @Inject role, than weld does also create a new Instance within the class User even I do not need it.


              Does Weld @Inject handles the NullObjectPattern internally?