2 Replies Latest reply on Jun 15, 2009 9:52 AM by wolfgangknauf

    how to get principal from Entity

      I know the more accepted pattern is to not have logic into the entity. However I am performing the migration of an application from EJB2 to EJB3 where there is logic into the entities.

      In my old entity bean (not session bean), I have code that call getCallerPrincipal from the EntityContext. I have tried, using @Resource, to inject EntityContext but I got a null value. Is there a way to access the callerPrincipal from entity in EJB3.

      Thank you very much for your help,

      An Phong Do

        • 1. Re: how to get principal from Entity
          rwiesemann

          Hi,
          i have a similar requirement to lookup the caller principal in an entity bean.

          Have you got (or found) a solution?

          Thanks

          Roland Wiesemann

          • 2. Re: how to get principal from Entity
            wolfgangknauf

            Hi Roland,

            I think that "EntityContext" is EJB2 and no longer supported in EJB3. You can get this information from the SessionContext in a SessionBean:

            @Resource()
            private SessionContext sessionContext = null;
            
            public void someSessionBeanMethod()
            {
             Principal principal = this.sessionContext.getCallerPrincipal();
             ...
            }
            



            Or maybe you could follow this post: http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3968882
            It seems that you can use entity callbacks like "@PreUpdate" and use the class "org.jboss.security.SecurityAssociation" to get the Principal:
            @PreUpdate
            private void someMethod()
            {
             Principal principal = SecurityAssociation.getPrincipal();
             ...
            }


            Hope this helps

            Wolfgang