использовать мой пользовательский перехватчик при использовании entitymanagerFactory - PullRequest
0 голосов
/ 16 декабря 2018

В нашем приложении для нашего дао мы вводим управляющий объект, используя контекст @Persistence.как показано ниже:

@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");
}
}

для того, чтобы внедрить мой перехватчик в фабрику сессий, мне нужно извлечь sessionFactory из entitymanagerFactory.но, к сожалению, мой перехватчик не выстрелил.Что я делаю не так?

...