[英]Create Update History Log Table, from JDBC - Java
我需要創建我通過JDBC對表進行的所有更新的歷史記錄。 例如,我有一個產品表,如果我在名稱列中進行更新,則需要將此更改記錄在歷史表中,但是,僅以下數據; 表名,列名和內容此列在更新后的內容之前。 通過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
這樣的事情。 我不知道從哪里開始?
如果您使用的是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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.