2016-08-24 80 views
0

我需要創建一個通過JDBC在表上執行的所有更新的歷史記錄。例如,我有一個產品表,如果我在名稱列中進行更新,則需要在歷史表中記錄此更改,但只有以下數據;表名稱,列的名稱以及此列在更新後的內容之前的內容。通過JDBC(Java)。從JDBC創建更新歷史記錄表 - Java

實施例表產品

productid|name  |value 
1  |computer |1000 

updated 
productid|name  |value 
1  |mouse  |10 

product log history 
table = product 
columnBefore name = computer 
columnAfter name = mouse 
columnBefore value = 1000 
columnAfter value = 10 

類似的東西。 我不知道從哪裏開始 有什麼想法嗎?

+0

是否需要通過JDBC創建歷史記錄?數據庫觸發是一個選項嗎?因此,您的代碼(或任何其他客戶端)執行UPDATE,並且數據庫觸發器會記錄更改。 –

+0

您正在使用哪些DBMS? Oracle(企業版)或DB2可以爲您的系統自動完成此操作。在Postgres中,您可以輕鬆創建將此信息寫入單獨表的審計觸發器。 –

+0

我正在使用Oracle 11g。我可以開發這個唯一的java代碼。我不能使用Trigger。我做的第一步。通過Apache庫比較兩個對象(舊的,新的)的區別。現在我需要發現如何註冊表名和列名。 – Anderson

回答

0

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

不幸的是,我們不能使用休眠 – Anderson

相關問題