8 Replies Latest reply on May 9, 2005 11:23 AM by rkrylov

    @Inject EntityContext ctx; --> doesn't work

    bigm25

      I'm trying to inject the EntityContext into my EntityBeans, so i could fill some audit-fields with the current principal.

      @Inject EntityContext ctx;

      However ctx is always null, the inject actually never happens.
      Is there something special with EntityBeans? Cause @Inject SessionContext works fine in SessionBeans....

      tia,

      Marco

        • 1. Re: @Inject EntityContext ctx; --> doesn't work
          dkrizic

          I think it would make more sense to put this logic into a Session Bean! This is where I use the @Injected EntityManager. Don't forget that Entity Beans are Value Beans and must be fully serializable!

          • 2. Re: @Inject EntityContext ctx; --> doesn't work
            bigm25

            Serialization is a good point, thx!!
            However, i wanted to achieve something like

            * @jboss.audit-created-by column-name="createdby"
            * @jboss.audit-created-time column-name="createdtime"
            * @jboss.audit-updated-by column-name="updatedby"
            * @jboss.audit-updated-time column-name="updatedtime"

            or are these "annotations" still working in JBoss-EJB3 ??

            Any other ideas on how to implement such audit fields in an EJB3 EntityBean (which chould be inherited to all EntityBeans....)?

            Otherwise i'd have to code it into all "create" / "upadte" methods of the SessionBeans....

            Marco

            • 3. Re: @Inject EntityContext ctx; --> doesn't work
              dkrizic

              Hi!

              Try something like this:

              @Entity
              @EntityListener("com.....common.management.GenericEntityLogger")
              @Table(name = "xuser")
              public class User implements Serializable, Displayable {


              public class GenericEntityLogger {

              @PostPersist
              public void doPostPersist( Object o ) {
              Logger log = Logger.getLogger( o.getClass() );
              log.info("Persisted " + o.getClass().getSimpleName() + " [" + o.toString() + "]");
              }
              }


              The method doPostPersist should be the right place to modify your fields!




              • 4. Re: @Inject EntityContext ctx; --> doesn't work
                bigm25

                Rehi!

                i know about the pre/post update/persist listeners. What i want to do is to save the pricipal.getName() of the current EntitContext, so i can trace who modified/created the entity.

                Is it possible to inject EntitContext or SessionContext into "GenericEntityLogger?

                • 5. Found a solution for auditing.... and maybe some bugs..
                  bigm25

                  hi,

                  found a quick (and dirty?) solution for my problem:
                  Since "@Inject SessionContext" is working in sessionbeans i wrote a SessionBeanInterceptor and upon every call to any "DAO" method I store the current SessionContext in a "static singleton" Map, using Thread.currentThread().getId() as the key. A few processor cycles later in my entitybean's "@prePersist" / "@preUpdate" methods i'm able to retrieve the sessioncontext out of the map by using again the thread's Id.

                  But why is there "javax.ejb.EntityContext" in EJB3 when there's no way to access it? Or is this a JBoss-EJB3 bug?

                  Another thing looks "bug-suspect":
                  My DAO sessionbeans all inherit from "MyAbstractSession". When in annotate this "BaseSession" with
                  "@Interceptors ({"de.eyetea.entity.PropagateSessionContextInterceptor"})"
                  this annotation is NOT inherited to the actual sessionbeans, instead i have to annotate each actual sessionbean instead.
                  However my entitybeans all inherit from "MyBaseEntity" which is annotated with "@EntityListener("de.eyetea.smartea.entity.AuditorEntityListener") "
                  ,and in this case all inheritig entitybeans call the listener without having to annotate each an every entitybean.
                  Why is it that "@EntityListener" is inherited, "@Interceptors" is not??

                  • 6. Re: @Inject EntityContext ctx; --> doesn't work
                    rkrylov

                    I made

                    @Inject
                    public static EntityManager entity_manager;
                    @Inject
                    public static TimerService timerService;
                    

                    in stateless session bean and access them from
                    entity as any statics:
                    my.some.pkg.Stateless.entity_manager ...
                    But there is another problem I encounter: timer.getHandle() throws exception: "java.lang.IllegalStateException: Cannot obtain inMethodFlag for: Timer.getHandle"
                    anybody suggest workaround?
                    Thanks.

                    • 7. Re: @Inject EntityContext ctx; --> doesn't work
                      bill.burke

                      you cannot inject into a static field

                      • 8. Re: @Inject EntityContext ctx; --> doesn't work
                        rkrylov

                        Forbidden by specification? :)
                        It works with EJB3_Preview5+jbossAS 4.0.2
                        in stateless bean.
                        I don't know how, but it works!