在使用entitymanagerFactory时使用我的自定义拦截器

时间:2018-12-16 13:04:10

标签: hibernate entitymanager sessionfactory

在dao中的应用程序中,我们使用@Persistence上下文注入entitymanager。如下所示:

@Repository
public class GroupDaoJpa implements GroupDao {

 @PersistenceContext
 private EntityManager entityManager;

 @Override
 public Group update(Group group) {
    return entityManager.merge(group);
 }
}

在我的模块加载的核心层中,我有:

<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="persistenceUnitName" value="chaparCoreUnit"/>
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="${db.showsql}"/>
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.default_schema">${db.schema}</prop>
        </props>
    </property>
</bean>

现在要审计更改,我需要像下面这样使用EmptyInterceptor:

public class LoggingInterceptor extends EmptyInterceptor {
private static final long serialVersionUID = 1L;



public void onDelete(Object entity,
                     Serializable id,
                     Object[] state,
                     String[] propertyNames,
                     Type[] types) {

    // log delete events
    System.out.println("Delete event");
}

// called when a Student gets updated.
public boolean onFlushDirty(Object entity,
                            Serializable id,
                            Object[] currentState,
                            Object[] previousState,
                            String[] propertyNames,
                            Type[] types) {
    if ( entity instanceof Group) {
        System.out.println("Student Update Operation");
        return true;
    }
    return false;
}

// called on load events
public boolean onLoad(Object entity,
                      Serializable id,
                      Object[] state,
                      String[] propertyNames,
                      Type[] types) {

    // log loading events
    System.out.println("Load Operation");
    return true;
}

// This method is called when Employee object gets created.
public boolean onSave(Object entity,
                      Serializable id,
                      Object[] state,
                      String[] propertyNames,
                      Type[] types) {
    if ( entity instanceof Group ) {
        System.out.println("Student Create Operation");
        return true;
    }
    return false;
}

//called before commit into database
public void preFlush(Iterator iterator) {
    System.out.println("Before commiting");
}

//called after committed into database
public void postFlush(Iterator iterator) {
    System.out.println("After commiting");
}
}

为了将拦截器注入会话工厂,我需要从entitymanagerFactory中提取sessionFactory。但不幸的是我的拦截器没有开火我在做什么错了?

0 个答案:

没有答案