<bean id="auditDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.audit.url}"/>
<property name="username" value="${jdbc.audit.username}"/>
<property name="password" value="${jdbc.audit.password}"/>
<property name="maxActive" value="100"/>
<property name="maxWait" value="1000"/>
<property name="poolPreparedStatements" value="true"/>
<property name="defaultAutoCommit" value="true"/>
<property name="validationQuery" value="${jdbc.validationQuery}"/>
<property name="testOnBorrow" value="true"/>
</bean>
<bean id="auditSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" destroy-method="destroy">
<property name="dataSource" ref="auditDataSource"/>
<property name="configLocation" value="classpath:hibernate.audit.cfg.xml"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<!--<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>-->
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<!-- Hibernate Search index directory -->
<prop key="hibernate.search.default.indexBase">${app.search.index.basedir}</prop>
</props>
<!-- Turn batching off for better error messages under PostgreSQL -->
<!-- hibernate.jdbc.batch_size=0 -->
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="auditTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="auditSessionFactory"/>
</bean>
<bean id="javersRepoConnectionProvider" class="com.dsc.discus.ng.audit.javers.JaversRepoConnectionProvider">
<constructor-arg name="dataSource" ref="auditDataSource"/>
</bean>
<bean id="auditController" class="com.dsc.discus.ng.audit.javers.JaversAuditController">
<constructor-arg name="javersRepoConnectionProvider" ref="javersRepoConnectionProvider"/>
<constructor-arg name="transactionManager" ref="auditTransactionManager"/>
</bean>
JaversAuditController bean:
public class JaversAuditController extends AuditController {
private final JaversSqlRepository javersSqlRepository;
private final Javers javers;
public JaversAuditController(JaversRepoConnectionProvider javersRepoConnectionProvider, PlatformTransactionManager transactionManager) {
javersSqlRepository = SqlRepositoryBuilder.sqlRepository()
.withConnectionProvider(javersRepoConnectionProvider)
.withDialect(DialectName.MYSQL)
.build();
javers = TransactionalJaversBuilder.javers()
.withTxManager(transactionManager)
.withObjectAccessHook(new HibernateUnproxyObjectAccessHook())
.withMappingStyle(MappingStyle.BEAN)
.registerJaversRepository(javersSqlRepository)
.build();
}
@Override
public void addModified(BaseEntity entity) {
addModified(entity, null);
}
@Override
public void addModified(BaseEntity entity, Map<String, String> extraParams) {
if (javers != null && entity != null) {
if (extraParams != null) {
javers.commit(entity.getModifiedBy(), entity, extraParams);
}
else {
javers.commit(entity.getModifiedBy(), entity);
}
}
}
public JaversSqlRepository getJaversSqlRepository() {
return javersSqlRepository;
}
public Javers getJavers() {
return javers;
}
我必须使用BEAN映射样式,因为JPA注释位于实体的访问器上。
下面是JaversRepoConnectionProvider bean:
public class JaversRepoConnectionProvider implements ConnectionProvider {
private DataSource dataSource;
public JaversRepoConnectionProvider(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
我在做人工审核。也就是说,每次保存实体时,我都会调用javers.commit()方法。碰巧的是,我尝试保存的第一件事就会导致一个MySQL语法错误,声明表“jv_commit”不存在。文档声明将自动创建必要的表,所以我一定遗漏了什么。请提前告知并致谢。
截断堆栈跟踪:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'discus_eng_audit.jv_commit' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2825)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2323)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.polyjdbc.core.transaction.Transaction.executeQuery(Transaction.java:59)
at org.polyjdbc.core.query.TransactionalQueryRunner.queryCollection(TransactionalQueryRunner.java:86)
at org.polyjdbc.core.query.TransactionalQueryRunner.queryList(TransactionalQueryRunner.java:76)
at org.javers.repository.sql.PolyUtil.queryForBigDecimalList(PolyUtil.java:27)
at org.javers.repository.sql.PolyUtil.queryForOptionalBigDecimal(PolyUtil.java:36)
at org.javers.repository.sql.repositories.CommitMetadataRepository.selectMaxCommitId(CommitMetadataRepository.java:86)
at org.javers.repository.sql.repositories.CommitMetadataRepository.getCommitHeadId(CommitMetadataRepository.java:75)
at org.javers.repository.sql.JaversSqlRepository.getHeadId(JaversSqlRepository.java:76)
at org.javers.repository.api.JaversExtendedRepository.getHeadId(JaversExtendedRepository.java:137)
at org.javers.core.commit.CommitIdFactory.nextId(CommitIdFactory.java:26)
at org.javers.core.commit.CommitFactory.newCommitMetadata(CommitFactory.java:79)
at org.javers.core.commit.CommitFactory.create(CommitFactory.java:69)
at org.javers.core.JaversCore.commit(JaversCore.java:82)
at org.javers.core.JaversCore.commit(JaversCore.java:67)
at org.javers.spring.jpa.JaversTransactionalDecorator.commit(JaversTransactionalDecorator.java:58)
at com.dsc.discus.ng.audit.javers.JaversAuditController.addModified(JaversAuditController.java:56)
at com.dsc.discus.ng.audit.javers.JaversAuditController.addModified(JaversAuditController.java:46)
at com.dsc.discus.ng.model.persistence.dao.hibernate.BaseDaoHibernate.save(BaseDaoHibernate.java:146)
at com.dsc.discus.ng.model.persistence.dao.hibernate.BaseDaoHibernate.save(BaseDaoHibernate.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
在Spring应用程序中,应该将Javers实例注册为Spring bean。JaVers使用@PostConstruct
注释,当txManager准备就绪时,Spring调用ensureschema()
。
参见JaversTransactionalDecorator Impl:
@PostConstruct
public void ensureSchema() {
TransactionTemplate tmpl = new TransactionTemplate(txManager);
tmpl.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
javersSqlRepository.ensureSchema();
}
});
}
在您的例子中,没有调用这个方法,因为您已经在控制器中创建了Javers实例,而不是作为Spring bean创建的。您应该按照docs的要求配置应用程序-https://javers.org/documentation/spring-integration/#jpa-entity-manager-integration
我在Spring Boot应用程序中有一个Javers实现。Mongo4.4被用作数据库。从MongoDB4.4开始,您可以在事务中创建文档。 我在创建对象时模拟了一个异常。如预期的那样,对象没有在数据库中创建,但是一个新的快照被添加到jv_snapshots集合中。 控制器:
Javers团队,我是托马斯,我正在使用Javers2.0.3,我得到以下错误 具有上述各自错误的沙威断裂
我做了一个重构,现在我的类B不再是一个实体,而是一个值。 如果我尝试查看一个已经审计的实体A的差异,它包含一个B的列表,我会得到这个异常:JaversException Managed_Class_Mapping_error:给定的javaClass“Class B”映射到ValueType,预期的是EntityType 有什么建议的方法来解决这个问题吗?
我想将审核更改持久化到oracle db。micro服务是一个带有hibernate的Spring Boot应用程序。我在javers的mvn存储库链接中发现了许多依赖项 我应该为oracle db使用哪个依赖项?
...除了这不存在。但这是关于我在这里想做什么。 或相反的: ...它获取每个GameCharacter的最早版本。为此,我已经尝试了,但显然Javers没有从最后开始计算版本。 这方面的Gradle依赖关系是:
我有两个列表,数据相同,但顺序不同。 结果: