6 Replies Latest reply on Sep 9, 2010 2:55 AM by nimo stephan

    static inner classes and weld

    nimo stephan Master

      I have something like this:


      public class User implements Serializable{
      
      private static final long serialVersionUID = -4445708786613946532L;
      
      private Role role;
      //getter/setter
      
      public static class Role
      {
      // Do I need that?
      private static final long serialVersionUID = -2224808788883946532L;
      ..
      }
      }



      and have the two questions:



      1. Question:
      As I use the User-class into a session scoped class, I need to have a serialVersionUID (for passivating beans). Do I need to have a serialVersionUID within my static inner class, too (in this case Role)?


      2. Question:
      Are there any caveats or cautions when using static inner classes with weld?
      Such as scoping, injecting or the like?



        • 1. Re: static inner classes and weld
          Pete Muir Master

          1) You never need a serialVersionUID, but it is recommended for any class that implements Serializable. Your inner class doesn't so you don't need it.


          2) All inner classes (static or not) support all CDI capabilities.

          • 2. Re: static inner classes and weld
            nimo stephan Master

            1) You never need a serialVersionUID, but it is recommended for any class that implements Serializable. Your inner class doesn't so you don't need it.


            In http://seamframework.org/Community/PassivatingScopeMustBePassivationCapableAndBeanIsNotProxyable it was said, that even pojos which are injected into a sessionscoped bean have to implement Serializable to avoid the org.jboss.weld.exceptions.UnproxyableResolutionException.


            So I have to implement all these pojos with Serializable only to avoid the weld-exception! I have no choice not to do that. Imagine 100 objects (pojos) injecting into session scoped bean. I have to change all these 100 objects only to fullfill the weld-requirement. Am I right?




            • 3. Re: static inner classes and weld
              nimo stephan Master

              So in my case, when injecting User into a sessionscoped-bean, User must implement Serializable.


              The question is, do I also have to implement Role with Serializable, when Role is a instance of User?


              I have discovered, that Role also needs to implement Serializable when @Injecting it into User, where User is also injected to another sessionscoped-bean:



              @SessionScoped
              public class MyBean implements Serializable{
              
              // inherits dependent scope (in this case: sessionscoped)
              @Inject User u;
              
              
              }
              
              // no scoped bean
              public class User implements Serializable{
              
              private static final long serialVersionUID = -4445708786613946532L;
              
              @Inject private Role role;
              //getter/setter
              
              
              // no scoped bean
              public static class Role implements Serializable
              {
              private static final long serialVersionUID = -2224808788883946532L;
              ..
              }
              }



              when instantiating Role with new-operator instead of injecting an new instance,
              then I do not have to implement Serializable:


              @SessionScoped
              public class MyBean implements Serializable{
              
              // inherits dependent scope (in this case: sessionscoped)
              @Inject User u;
              
              
              }
              
              // no scoped bean
              public class User implements Serializable{
              
              private static final long serialVersionUID = -4445708786613946532L;
              
              private Role role = new Role();
              //getter/setter
              
              
              // no scoped bean
              public static class Role
              {
              ..
              }
              }



              So are there any dangers or caveats with that?

              • 4. Re: static inner classes and weld
                Nicklas Karlsson Master

                Check out Weld source, remove validation, rebuild, update AS and disable passivation ;-)

                • 5. Re: static inner classes and weld
                  Wang Liyu Apprentice

                  if the Role is not injected from Session or Conversation scope, you can use transient, then you don't have to implement Serializable.

                  • 6. Re: static inner classes and weld
                    nimo stephan Master

                    if the Role is not injected from Session or Conversation scope


                    I do not want to use transient - in my eyes, in web-applications passivating beans is necessary (dont you think so?).



                    Role is annotated with no-scope. I have a User (also no-scoped) with a Role. I inject a User into a sessionscoped bean, hence User needs to implement Serializable (to enable passivation).


                    The questions are:


                    - Do my Role-Class also need to implement Serializable (to enable passivation) ?
                      I have only inject User (which has a property of Object Role) into
                      sessionscoped bean. Normally, Weld should throw me an UnproxyableResolutionException.


                    And the more interesting question is:


                    Imagine my User-Object does not implement Serializable:
                    If I create a User-Instance into my sessionscoped with new-Operator instead of injecting it, then I clearly get no UnproxyableResolutionException.
                    The question is: Does the passivation for my User-Instance works furthermore?
                    As User is only a pojo (Entity Bean) and not a service, I want to prefer to create a new instance with new-Operator instead of injecting it.



                    yes, I can use transient to avoid to implement Serializable. But why should I?