2016-08-24 11 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(Enterprise Edition)またはDB2は完全に自動的にそれを行うことができます。 Postgresでは、この情報を別のテーブルに書き込む監査トリガを簡単に作成できます。 –

+0

私はOracle 11gを使用しています。私はこの唯一のJavaコードを開発することができます。私はトリガーを使用することはできません。私がやった最初のステップ。 2つのオブジェクト(古いもの、新しいもの)の違いをApacheライブラリで比較する。今私はテーブルの名前と列の名前を登録する方法を発見する必要があります。 – Anderson

答えて

0

Hibernateを使用している場合は、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

関連する問題