5 Replies Latest reply on Mar 3, 2011 4:27 PM by sean.tozer

    Are @EntityListeners with injection possible?

    craig

      Hi i have an @EntityListener on an entity that listens to PrePersist events.  The method gets called fine however, done of my injected properties are set. 


      I can get one by doing



      Account account = (Account) Contexts.getSessionContext().get("account");
      



      but how do i get my entityManager?


      Thanks


      Craig

        • 1. Re: Are @EntityListeners with injection possible?
          keithnaas

          Craig,


          Don't do annotation based injection in an EntityListener.  Last time I checked, the lifecycle of the object is not controlled by Seam. 


          Try


          Component.getInstance("entityManager");//where entityManager is the name of the entityManager in your components.xml
          


          • 2. Re: Are @EntityListeners with injection possible?
            craig

            Thats great works well.


            Is this a good design pattern or should i be using seams event model and raising prePersist events?


            I dont want to use the prePersist annotation in my entity as i need to access the db for get a specific value at point of persist.


            Thanks


            Craig



            • 3. Re: Are @EntityListeners with injection possible?
              keithnaas

              Hmmm...I suppose it depends on what kind of logic will be executing in prepersist.  If its simply a matter of putting some audit information somewhere...just use a prepersist.  But if you need to do something really crazy, I would probably look at using seam events.  Unfortunately, there's a million way to skin this cat.

              • 4. Re: Are @EntityListeners with injection possible?
                monkeyden

                Seam 2.0.3.CR1


                I tried this with:


                identity = (Identity)Component.getInstance("identity");



                also tried the more verbose variety:


                identity = (Identity)Component.getInstance("org.jboss.seam.security.identity");



                and identity comes up null.  It does get into the method ok.  Any ideas?

                • 5. Re: Are @EntityListeners with injection possible?
                  sean.tozer

                  This is an old thread, but I came across it while looking for a solution, and wanted to post back how I eventually solved it.


                  Identity identity = (Identity)Component.getInstance(Identity.class);


                  works to manually inject the identity component, while the string identifier does not.