package com.easou.ad.listener;
import java.util.Date;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.event.PostDeleteEvent;
import org.hibernate.event.PostDeleteEventListener;
import org.hibernate.event.PostInsertEvent;
import org.hibernate.event.PostInsertEventListener;
import org.hibernate.event.PostUpdateEvent;
import org.hibernate.event.PostUpdateEventListener;
import com.easou.ad.entity.AdOperate;
public class SaveOrUpdateListener implements PostInsertEventListener ,PostUpdateEventListener, PostDeleteEventListener {
private static final long serialVersionUID = 1L;
Logger log = LogManager.getLogger(SaveOrUpdateListener.class);
public void onPostInsert(PostInsertEvent event) {
AdOperate entry = new AdOperate();
entry.setType(OperationType.CREATE);
entry.setDatetime(new Date());
entry.setEntityName(event.getEntity().getClass().getName());
saveOperate(event.getSession(), entry);
log.debug(entry);
}
public void onPostUpdate(PostUpdateEvent event) {
for (int i = 0; i < event.getState().length; i++) {
// 更新前的??
Object oldValue = event.getOldState()[i];
// 更新后的新???
Object newValue = event.getState()[i];
// 跳过集合属???
if (newValue instanceof PersistentCollection) {
continue;
}
if (oldValue != null && !oldValue.equals(newValue)) {
AdOperate entry = new AdOperate();
// 取得属???名??
entry.setProperty(event.getPersister().getPropertyNames()[i]);
entry.setType(OperationType.UPDATE);
// 如果更改的属性是关联对象,则存储其id
//if (oldValue instanceof AbstractEntity) {
// entry.setBeforeValue(((AbstractEntity) oldValue).getId().toString());
//} else {
entry.setBeforeValue(oldValue != null ? oldValue.toString() : null);
// }
//if (newValue instanceof AbstractEntity) {
// entry.setAfterValue(((AbstractEntity) newValue).getId().toString());
// } else {
entry.setAfterValue(newValue != null ? newValue.toString() : null);
// }
entry.setDatetime(new Date());
entry.setEntityName(event.getEntity().toString());
saveOperate(event.getSession(), entry);
log.debug(entry);
}
}
}
public void onPostDelete(PostDeleteEvent event) {
AdOperate entry = new AdOperate();
entry.setType(OperationType.DELETE);
entry.setDatetime(new Date());
entry.setEntityId(new Long(event.getId().toString()));
entry.setEntityName(event.getEntity().getClass().getName());
saveOperate(event.getSession(), entry);
log.debug(entry);
}
private void saveOperate(Session session, AdOperate entry) {
Session tempSession = session.getSessionFactory().openSession();
Transaction tx = tempSession.beginTransaction();
try {
tx.begin();
tempSession.save(entry);
// tempSession.flush();
// tx.commit();
} catch (Exception ex) {
tx.rollback();
}
tempSession.close();
}
}
spring的配置
<property name="eventListeners">
<map>
<entry key="post-commit-insert"><bean class="com.easou.ad.listener.SaveOrUpdateListener" /></entry>
<!-- <entry key="post-commit-update"><bean class="com.easou.ad.listener.SaveOrUpdateListener" /></entry>-->
<entry key="post-commit-delete"><bean class="com.easou.ad.listener.SaveOrUpdateListener" /></entry>
</map>
</property>
结果会插入N条 AdOperate 的记录