2016-08-24 2 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를 사용하고 있습니다. 이 자바 코드 만 개발할 수 있습니다. 트리거를 사용할 수 없습니다. 내가 한 첫 걸음. Apache 라이브러리를 통해 두 객체 (old, new)의 차이점을 비교하십시오. 이제는 테이블 이름과 컬럼 이름을 등록하는 방법을 알아야합니다. – Anderson

답변

0

최대 절전 모드를 사용하는 경우, 인터셉터를 시작할 수 있습니다. 당신은 당신의 목적을 위해 아래의 메소드를 오버라이드 할 수 있습니다.

추출 된 메소드는 시나리오에 필요한 인터셉터 인터페이스에 속합니다.

/** 
* 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