7

I'm working with a spring configured hibernate application. There is transactionmanagement and an auditInterceptor defined as entityInterceptor. When I debug the code I'm entering the entityInterceptors methods and the date's are being set, however at the end of the save they are not in the database :(.

Consider following configuration

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">     
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=${hibernate.dialect}
                hibernate.show_sql=${hibernate.show_sql}
                hbm2ddl.auto=${hbm2ddl.auto}
            </value>
        </property>
        <property name="schemaUpdate">
            <value>true</value>
        </property>
        <property name="annotatedClasses">
            <list>
                                .. bunch of annotatedClasses" ...
            </list>
        </property>
    </bean>

<bean name="auditInterceptor" class="com.mbalogos.mba.dao.AuditInterceptor" />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="entityInterceptor" ref="auditInterceptor"/>
    </bean>

    <bean id="namedQueryDao" class="com.mbalogos.mba.dao.NamedQueryDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

and following entityInterceptor

public class AuditInterceptor extends EmptyInterceptor{

    /**
     * 
     */
    private static final long serialVersionUID = -8374988621501998008L;

    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state,
            String[] propertyNames, Type[] types) {
        if(entity instanceof DomainObject){
            Timestamp date = new Timestamp(new Date().getTime());
            ((DomainObject)entity).setCreationDate(date);
            ((DomainObject)entity).setModificationDate(date);
        }       
        return true;
    }

    @Override
    public boolean onFlushDirty(Object entity, Serializable id,
            Object[] currentState, Object[] previousState,
            String[] propertyNames, Type[] types) {
        if(entity instanceof DomainObject){
            DomainObject domainObject = (DomainObject)entity;
            Timestamp date = new Timestamp(new Date().getTime());
            domainObject.setModificationDate(date);             
        }
        return true;
    }

    @Override
    public void onDelete(Object entity, Serializable id, Object[] state,
            String[] propertyNames, Type[] types) {
        super.onDelete(entity, id, state, propertyNames, types);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void preFlush(Iterator entities) {
        super.preFlush(entities);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void postFlush(Iterator entities) {
        super.postFlush(entities);
    }
}

following save method, sessionFactory is injected in the class

public <T extends DomainObject> T save(T objectToSave) {
    Session currentSession = null;
    try {
        currentSession = sessionFactory.getCurrentSession();
        currentSession.save(objectToSave);

        return objectToSave;
    } catch (Exception ex) {
        logger.error(ex);
    }
    return null;
}

Anyone has any idea why this behaviour is happening. Oh I also tried putting the entityInterceptor in the sessionFactory instead of the transactionmanager that was my first try , same behaviour :(

4

2 に答える 2

12

私はそれを理解することができました.エンティティオブジェクトではなく、プロパティ名とその状態をいじる必要がありました..

@Override
public boolean onFlushDirty(Object entity, Serializable id,
        Object[] currentState, Object[] previousState,
        String[] propertyNames, Type[] types) {
    return audit(currentState, propertyNames);              
}

@Override
public boolean onSave(Object entity, Serializable id, Object[] state,
        String[] propertyNames, Type[] types) {
    return audit(state, propertyNames);
}

private boolean audit(Object[] currentState, String[] propertyNames) {
    boolean changed = false;
    Timestamp timestamp = new Timestamp(new Date().getTime());
    for(int i=0;i<propertyNames.length;i++){
        if("creationDate".equals(propertyNames[i])){
            Object currentDate = currentState[i];
            if(currentDate == null){
                currentState[i] = timestamp;
                changed = true;
            }
        }

        if("modificationDate".equals(propertyNames[i])){
            currentState[i] = timestamp;
            changed = true;
        }
    }
    return changed;
}
于 2012-08-03T18:01:25.837 に答える
2

ありがとうケニー、私も同じ問題に直面していました。私の場合、インターセプターは一部のエンティティで機能し、残りのエンティティでは機能していません。
考えられる最適化の例:
* 両方のプロパティの検索が終了したら、ループを中断します。
* DomainObject のみで監査メソッドを適用する場合は、このメソッドを次のようにフィルタリングしますif(entity instanceof DomainObject)

それでも、エンティティ オブジェクトに属性を直接設定しても一部のエンティティで機能しなかった理由が気になります。あなたまたは誰かが理由を知っている場合は、ここに投稿してください。

于 2012-11-05T15:33:15.263 に答える