2016-08-24 83 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

相关问题