4 Replies Latest reply on Dec 16, 2007 3:26 PM by Tobias Hill

    Set non-shared Hibernate interceptor on JPA EntityManager

    Micah Schehl Newbie

      I am using Seam 1.2 with JPA and need to assign a thread-safe Interceptor to my Hibernate sessions. I know I can set thread-unsafe interceptors in the persistance.xml by setting the property "hibernate.ejb.interceptor", but is there a way to run something equivalent to the code below to create the EntityManager?

      MyInterceptor interceptor = new MyInterceptor();
      Session session = getSessionFactory().openSession(interceptor);
      Transaction tx = session.beginTransaction();
      interceptor.setSession(session);
      interceptor.setUsername(identity.getUsername());
      



        • 1. Re: Set non-shared Hibernate interceptor on JPA EntityManage
          Micah Schehl Newbie

          Maybe I can use thread-safe Interceptors. Is it safe and okay to do the following?

          public class Mynterceptor extends EmptyInterceptor {
          
           @In Identity identity;
          
           @PersistenceContext
           private EntityManager em;
          
           ...
          }
          




          • 2. Re: Set non-shared Hibernate interceptor on JPA EntityManage
            Micah Schehl Newbie

            I believe my thread-safe code is not okay because I want to create and save new entities inside of it.

            To do that safely, I would need to create a new temporary EntityManager using the same JDBC connection and transaction.

            How can I do that? Other than that, is the code safe and okay to do?

            public class MyInterceptor extends EmptyInterceptor {
            
             @In Identity identity;
            
             @PersistenceContext
             private EntityManager em;
            
             ...
            }



            • 3. Re: Set non-shared Hibernate interceptor on JPA EntityManage
              Micah Schehl Newbie

              I tried using the thread-unsafe interceptors in the persistance.xml by setting the property "hibernate.ejb.interceptor" and the code below.

              I encountered two problems which I need help with:
              1. The afterTransactionBegins never gets called
              2. When the other methods get called, such as onSave, the identity and em don't get injected.

              Could somebody please shed some light on this for me? I do not have to use JPA if that is a limitation.

              Here is an explanation of what I am ultimately trying to achieve. If what I am trying to achieve can be done better in a different way, I would be okay with that.


              Whenever a transaction starts, I need to create an Audit All entities annotated with @Historizable that are inserted or updated in that transaction must have their audit property set to that Audit updated on them. Additionally, for each insert, update, and delete, a HistoryEntry gets saved that stores each entity's class name, primary key, audit id, and toString(). The audit and history needs to be part of the main transaction so that it also rolls back if the transaction fails.


              public class MyInterceptor extends EmptyInterceptor {
              
               @In Identity identity;
              
               @PersistenceContext
               private EntityManager em;
              
               ...
              }


              • 4. Re: Set non-shared Hibernate interceptor on JPA EntityManage
                Tobias Hill Newbie

                I am in need of auditlogging and versioning as well in the application we're building. And as always: the more elegant and slim solution, the better.

                So I wonder: Did you ever solve this in a nice way?