4 Replies Latest reply on Oct 21, 2008 6:19 AM by Drew Kutchar

    Component.getInstance insided a @PrePersist method

    Jason Long Novice

      I have been working on some simple auditing from my application.  Currently I just want to save the user that persisted the object.

      I tried using Component.getInstance inside a @PrePersist and it worked like a charm.  If the creator of the object is set already then that is used.  If not the current user is looked up from the session.

      So far this works like a charm and adds this to my application in a non intrusive way.

      Is this bad design or are there any caveats that I am overlooking?

      See code sample below.

          private void autoSetCreated()
            if(this.created==null) this.created=new Date();
            if(this.creator==null && this.id==null)
              this.creator=(Usr) Component.getInstance("currentUsr", ScopeType.SESSION);