[英]Hibernate Envers with Spring Boot - configuration
I'm trying to setup Hibernate Envers to work with my Spring Boot application. 我正在尝试设置Hibernate Envers以使用我的Spring Boot应用程序。
I've included the Envers dependency and added @Audited annotations and it works fine, but I'm unable to configure specific Envers properties, Spring Boot doesn't seem to pick them up. 我已经包含了Envers依赖项并添加了@Audited注释,它工作正常,但我无法配置特定的Envers属性,Spring Boot似乎没有接受它们。
Specifically, I've tried to set the different db schema for audit tables by putting these to application.properties , but without luck: 具体来说,我试图通过将这些设置为application.properties来为审计表设置不同的数据库模式,但没有运气:
hibernate.envers.default_schema=app_audit
or 要么
org.hibernate.envers.default_schema=app_audit
or 要么
spring.jpa.hibernate.envers.default_schema=app_audit
Neither of these work. 这些都不奏效。 Does anyone know how to set these? 有谁知道如何设置这些?
EDIT. 编辑。
As M. Deinum suggested I tried: 正如M. Deinum建议我尝试的那样:
spring.jpa.properties.org.hibernate.envers.default_schema=app_audit
and it worked! 它工作了!
For all those configuration settings that aren't by default available you can specify them by simply prefixing them with spring.jpa.properties
. 对于默认情况下不可用的所有配置设置,您可以通过在spring.jpa.properties
前面添加前缀来指定它们。 Those properties will be added, as is, to the EntityManagerFactory
(as JPA Properties). 这些属性将按原样添加到EntityManagerFactory
(作为JPA属性)。
spring.jpa.properties.org.hibernate.envers.default_schema=app_audit
Adding the above to the application.properties
will add the properties and should configure Hibernate Envers. 将上面的内容添加到application.properties
将添加属性并应配置Hibernate Envers。
This is also documented in the Spring Boot reference guide . Spring Boot 参考指南中也记录了这一点。
Links 链接
Looking through the HibernateJpaAutoConfiguration class I can't see any support for envers properties. 通过HibernateJpaAutoConfiguration类,我看不到对envers属性的任何支持。 The following might not be the best solution but nevertheless your can give it a try. 以下可能不是最佳解决方案,但您可以尝试一下。
In order to have Spring Boot support the envers properties you have to: 为了让Spring Boot支持你需要的envers属性:
override the current AutoConfiguration class that Spring Boot uses to configure the Hibernate properties, so it will read the envers properties from your property files. 覆盖Spring Boot用于配置Hibernate属性的当前AutoConfiguration类,因此它将从属性文件中读取envers属性。 This will read the spring.jpa.hibernate.envers.default_schema from your file and add it to the properties of the entityManagerFactoryBean : 这将从您的文件中读取spring.jpa.hibernate.envers.default_schema并将其添加到entityManagerFactoryBean的属性中:
@Configuration public class HibernateEnversAutoConfiguration extends HibernateJpaAutoConfiguration { private RelaxedPropertyResolver environment; public HibernateEnversAutoConfiguration() { this.environment = null; } @Override public void setEnvironment(Environment environment) { super.setEnvironment(environment); this.environment = new RelaxedPropertyResolver(environment, "spring.jpa.hibernate."); } @Override protected void configure(LocalContainerEntityManagerFactoryBean entityManagerFactoryBean) { super.configure(entityManagerFactoryBean); Map<String, Object> properties = entityManagerFactoryBean.getJpaPropertyMap(); properties.put("hibernate.envers.default_schema", this.environment.getProperty("envers.default_schema")); } }
exclude the original HibernateJpaAutoConfiguration that Spring Boot uses and add your own as a bean so it will be replaced: 排除Spring Boot使用的原始HibernateJpaAutoConfiguration并添加您自己的bean作为bean,因此它将被替换:
@EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class) @EnableJpaRepositories(basePackages = "com.gabrielruiu.test") @EntityScan(basePackages = "com.gabrielruiu.test") @ComponentScan(basePackages = "com.gabrielruiu.test") @Configuration public class Main { public static void main(String[] args) { SpringApplication.run(Main.class, args); } @Bean public HibernateEnversAutoConfiguration hibernateEnversAutoConfiguration() { return new HibernateEnversAutoConfiguration(); } }
For those using MySQL and Spring Boot , the suggestion of using: 对于使用MySQL和Spring Boot的用户 ,建议使用:
spring.jpa.properties.org.hibernate.envers.default_schema=yourAuditSchema
will not work. spring.jpa.properties.org.hibernate.envers.default_schema=yourAuditSchema
不起作用。
Use this instead: 请改用:
spring.jpa.properties.org.hibernate.envers.default_catalog=yourAuditSchema
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.