如果您使用的是Hibernate,您可以从Interceptor开始。为了您自己的目的,您可以重写以下方法。
我提取的方法属于您需要的场景的Interceptor接口。
/**
* Called just before an object is initialized. The interceptor may change the <tt>state</tt>, which will
* be propagated to the persistent object. Note that when this method is called, <tt>entity</tt> will be
* an empty uninitialized instance of the class.
*
* @return <tt>true</tt> if the user modified the <tt>state</tt> in any way.
*/
public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException;
/**
* Called when an object is detected to be dirty, during a flush. The interceptor may modify the detected
* <tt>currentState</tt>, which will be propagated to both the database and the persistent object.
* Note that not all flushes end in actual synchronization with the database, in which case the
* new <tt>currentState</tt> will be propagated to the object, but not necessarily (immediately) to
* the database. It is strongly recommended that the interceptor <b>not</b> modify the <tt>previousState</tt>.
*
* @return <tt>true</tt> if the user modified the <tt>currentState</tt> in any way.
*/
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) throws CallbackException;
/**
* Called before an object is saved. The interceptor may modify the <tt>state</tt>, which will be used for
* the SQL <tt>INSERT</tt> and propagated to the persistent object.
*
* @return <tt>true</tt> if the user modified the <tt>state</tt> in any way.
*/
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException;
/**
* Called before an object is deleted. It is not recommended that the interceptor modify the <tt>state</tt>.
*/
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException;
/**
* Called before a flush
*/
public void preFlush(Iterator entities) throws CallbackException;
/**
* Called after a flush that actually ends in execution of the SQL statements required to synchronize
* in-memory state with the database.
*/
public void postFlush(Iterator entities) throws CallbackException;
是否需要通过JDBC创建历史记录?数据库触发是一个选项吗?因此,您的代码(或任何其他客户端)执行UPDATE,并且数据库触发器会记录更改。 –
您正在使用哪些DBMS? Oracle(企业版)或DB2可以为您的系统自动完成此操作。在Postgres中,您可以轻松创建将此信息写入单独表的审计触发器。 –
我正在使用Oracle 11g。我可以开发这个唯一的java代码。我不能使用Trigger。我做的第一步。通过Apache库比较两个对象(旧的,新的)的区别。现在我需要发现如何注册表名和列名。 – Anderson