移植envers修正テーブルの既存のデータから、Hibernateの主体
-
23-08-2019 - |
質問
私は追加envers既存のhibernateます。すべてはスムーズに作動いて監査し問い合わせは異なる問題が修正テーブルな人口を既存のデータです。は誰で解決す。だがその場で発音を確認することがあるの移植を修正テーブルの既存のテーブルは?うん、いっそう便利です。
解決
あなたがする必要はありません。
AuditQueryはあなたによってRevisionEntityとデータのリビジョンの両方を取得することができます:
AuditQuery query = getAuditReader().createQuery()
.forRevisionsOfEntity(YourAuditedEntity.class, false, false);
この[3]オブジェクトのリストを返すクエリを構築します。・ファースト要素がデータであり、第二は、リビジョン・エンティティと第三のリビジョンのタイプである。
他のヒント
我々は、彼らがちょうど同じ時に作成されたかのように、すべての既存のエンティティを「挿入」をシミュレートするために、生のSQLクエリのシリーズを実行することによって、初期データを読み込ま。たとえばます:
insert into REVINFO(REV,REVTSTMP) values (1,1322687394907);
-- this is the initial revision, with an arbitrary timestamp
insert into item_AUD(REV,REVTYPE,id,col1,col1) select 1,0,id,col1,col2 from item;
-- this copies the relevant row data from the entity table to the audit table
の REVTYPE の値は、(修飾とは対照的に)挿入物を示すためにの0のであることに留意されたいです。
い問題がこのカテゴリーをご利用の場合Envers ValidityAuditStrategy しているデータが作成された以外のとEnversを有効にします。
我々の場合(Hibernate4.2.8.最終)の基本的なオブジェクトを更新す"は更新前の改正のためのエンティティ"(ログインしています。hibernate.AssertionFailure]HHH000099).
私がこの議論で説明でクロスibpc貿易引合掲示板
私たちは、次のように既存のデータと監査ログを取り込むの問題を解決した。
SessionFactory defaultSessionFactory;
// special configured sessionfactory with envers audit listener + an interceptor
// which flags all properties as dirty, even if they are not.
SessionFactory replicationSessionFactory;
// Entities must be retrieved with a different session factory, otherwise the
// auditing tables are not updated. ( this might be because I did something
// wrong, I don't know, but I know it works if you do it as described above. Feel
// free to improve )
FooDao fooDao = new FooDao();
fooDao.setSessionFactory( defaultSessionFactory );
List<Foo> all = fooDao.findAll();
// cleanup and close connection for fooDao here.
..
// Obtain a session from the replicationSessionFactory here eg.
Session session = replicationSessionFactory.getCurrentSession();
// replicate all data, overwrite data if en entry for that id already exists
// the trick is to let both session factories point to the SAME database.
// By updating the data in the existing db, the audit listener gets triggered,
// and inserts your "initial" data in the audit tables.
for( Foo foo: all ) {
session.replicate( foo, ReplicationMode.OVERWRITE );
}
(スプリングを介して)私のデータソースの構成
<bean id="replicationDataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value=".."/>
<property name="username" value=".."/>
<property name="password" value=".."/>
<aop:scoped-proxy proxy-target-class="true"/>
</bean>
<bean id="auditEventListener"
class="org.hibernate.envers.event.AuditEventListener"/>
<bean id="replicationSessionFactory"
class="o.s.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="entityInterceptor">
<bean class="com.foo.DirtyCheckByPassInterceptor"/>
</property>
<property name="dataSource" ref="replicationDataSource"/>
<property name="packagesToScan">
<list>
<value>com.foo.**</value>
</list>
</property>
<property name="hibernateProperties">
<props>
..
<prop key="org.hibernate.envers.audit_table_prefix">AUDIT_</prop>
<prop key="org.hibernate.envers.audit_table_suffix"></prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="post-insert" value-ref="auditEventListener"/>
<entry key="post-update" value-ref="auditEventListener"/>
<entry key="post-delete" value-ref="auditEventListener"/>
<entry key="pre-collection-update" value-ref="auditEventListener"/>
<entry key="pre-collection-remove" value-ref="auditEventListener"/>
<entry key="post-collection-recreate" value-ref="auditEventListener"/>
</map>
</property>
</bean>
インターセプターます:
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
..
public class DirtyCheckByPassInterceptor extends EmptyInterceptor {
public DirtyCheckByPassInterceptor() {
super();
}
/**
* Flags ALL properties as dirty, even if nothing has changed.
*/
@Override
public int[] findDirty( Object entity,
Serializable id,
Object[] currentState,
Object[] previousState,
String[] propertyNames,
Type[] types ) {
int[] result = new int[ propertyNames.length ];
for ( int i = 0; i < propertyNames.length; i++ ) {
result[ i ] = i;
}
return result;
}
}
PS:これは簡単な例であることに留意してください。これは、箱から出して動作しませんが、それは働いて解決に向けてご案内いたします。
http://www.jboss.orgを見てみましょう/files/envers/docs/index.html#revisionlogする
基本的には、@RevisionEntityアノテーションを使用して、独自の「リビジョンタイプ」を定義することができます その後、あなたの追加の監査データを挿入するためにRevisionListenerインタフェースを実装し、 現在のユーザーとハイレベルの操作などがあります。通常、これらはThreadLocalの文脈から引き出されます。