首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从JDBC - Java创建更新历史日志表

从JDBC - Java创建更新历史日志表
EN

Stack Overflow用户
提问于 2016-08-25 04:54:54
回答 1查看 956关注 0票数 0

我需要创建我通过JDBC在表上进行的所有更新的历史记录。例如,我有一个product表,如果我在name列中进行更新,则需要在历史表中记录此更改,但是,只有以下数据:表名、列名和content这一列在更新后的内容之前。通过JDBC (Java)。

示例表产品

代码语言:javascript
复制
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

差不多吧。我不知道从哪里开始有什么想法好吗?

EN

回答 1

Stack Overflow用户

发布于 2016-08-25 05:09:46

如果你正在使用Hibernate,你可以从Interceptor开始。你可以根据自己的目的重写下面的方法。

我提取的方法属于你在场景中需要的Interceptor接口。

代码语言:javascript
复制
/**
 * 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39132861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档