如果您使用的是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