Announcement Announcement Module
Collapse
No announcement yet.
Injecting EntityManager into EntityListener Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injecting EntityManager into EntityListener

    I have created a listener to audit entity objects as they are saved and updated:

    Code:
    @EntityListeners(value = {MyEntityListener.class})
    
    ...
    
    public class MyEntityListener {
      private EntityManager entityManager;
      @PersistenceContext
      public void setEntityManager(EntityManager em) {
        this.entityManager = em;
      }
      @PrePersist
      public void auditInsert(Object o) {
        // use this.entityManager here
      }
      @PreUpdate
      public void auditUpdate(Object o) {
        // use this.entityManager here
      }
    }
    So far, everything I've done to inject the EntityManager has failed because entityManager remains null. Is there a way to do this? Thanks.
    Last edited by socotech; Jan 11th, 2008, 09:06 AM.

  • #2
    EntityListeners are instanciated by JPA, so Spring does not have an opportunityto inject EntityManager. I would reconsider the use of an EntityManager from within an EntityListener, but you could try a ThreadLocal if you really need to make it happen.

    Comment


    • #3
      Fix for EntityManager NullPointerException

      Here is how to fix the EntityManager NullPointerException. Add the following line to your applicationContext.xml file:

      <context:annotation-config />

      Comment


      • #4
        Originally posted by rubens View Post
        Here is how to fix the EntityManager NullPointerException. Add the following line to your applicationContext.xml file:

        <context:annotation-config />
        This doesn't appear to work for me, should it?

        Comment


        • #5
          Hi All,

          Im also having the same problem, and I have the "<context:annotation-config />" in my configuration.
          I have the @Repository Annotation on the class, The @Transactional on the method and the @PersistenceContect on the setEntityManager method. but my EM still stays null.

          PS. Im trying to get this to work within an EJB environment, not a webapp / MVC environment

          any ideas?

          Comment


          • #6
            I'm still new to the JPA thing, but it seems like a deficiency in the JPA spec if the EntityManager is not accessible from within the @Pre* and @Post* methods. Without access to the EM you are pretty much limited to operating on simple properties. If I can't access the EM, then I'm unable to connect related entities to the open session. In my application, the currentUser is a persistent entity. When I try to set the createdBy property in the @PrePersist method, I get complaints about the user entity being detached when the persist() executes.

            As an alternative to the ThreadLocal<EntityManager> approach, there is the the opportunity to do pre- and post-processing in the DAO (@Repository) class. Unfortunately, the DAO instances won't get notified of cascaded EM persistence operations like the listeners do.

            Comment

            Working...
            X