Results 1 to 6 of 6

Thread: Injecting EntityManager into EntityListener

  1. #1
    Join Date
    Aug 2004
    Location
    Dallas, TX
    Posts
    26

    Default 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 at 09:06 AM.

  2. #2
    Join Date
    Oct 2007
    Posts
    1

    Default

    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.

  3. #3
    Join Date
    Apr 2007
    Location
    Dallas, TX, USA
    Posts
    13

    Smile Fix for EntityManager NullPointerException

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

    <context:annotation-config />

  4. #4

    Default

    Quote 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?

  5. #5
    Join Date
    Dec 2008
    Posts
    2

    Default

    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?

  6. #6
    Join Date
    Feb 2010
    Posts
    1

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •