6

我有一个带有 Hibernate 5.2.6 和 Spring data Envers 1.0.5 的 Spring boot 1.4.2 应用程序。我正在审核我的实体,并且审核记录已正确保存。

我的应用程序配置类被注释为使用 EnversRevisionRepositoryFactoryBean.class 作为 JPA 存储库工厂。

应用程序配置

@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)
@EnableTransactionManagement
public class ApplicationConfig {}

我正在尝试阅读被审计实体的修订。实体存储库扩展了 RevisionRepository。

实体模型

@Entity(name = "Base")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
@Table(name = "BASE")
@Audited
public abstract class Base {

  @Id
  @GeneratedValue(generator = "baseSeq", strategy = GenerationType.SEQUENCE)
  @SequenceGenerator(name = "baseSeq", sequenceName = "BASE_SEQ", allocationSize = 1)
  @Column(name = "id", updatable = false, nullable = false)
  private Long id;

  @Column(name = "name", nullable = false)
  private String name;

  @Column(name = "name", nullable = false)
  private long barId;

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public long getBarId() {
    return barId;
  }

  public void setBarId(long barId) {
    this.barId = barId;
  }

  public abstract String getType();

}

@Entity
@DiscriminatorValue("1")
@Audited
@NamedQueries({
    @NamedQuery(
        name = "Foo.findById",
        query = "select f from Base b where b.id = ?1"),
    @NamedQuery(
        name = "Foo.findByBarId",
        query = "select f from Base b where b.barId = ?1")})
public class Foo extends Base {
    private String type = "Foo";

    @Override
    public String getType() {
      return type;
    }
}

实体存储库

interface FooRepository extends JpaRepository<Foo, Long>,
    JpaSpecificationExecutor<Foo>, RevisionRepository<Foo, Long, Integer> {

  foo findById(Long Id);

  foo findByBarId(Long barId);

}

应用程序启动失败,因为由于 PropertyReferenceException 而无法初始化存储库。

原因:org.springframework.data.mapping.PropertyReferenceException:没有 找到类型 Foo 的属性 findRevisions! 在 org.springframework.data.mapping.PropertyPath.(PropertyPath.java:77) 在 org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) 在 org.springframework.data.mapping.PropertyPath.create( PropertyPath.java:309) 在 org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) 在 org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) 在 org.springframework.data .repository.query.parser.Part.(Part.java:76) 在 org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:235) 在 org.springframework.data.repository.query .parser.PartTree$Predicate.buildTree(PartTree.java:373) 在 org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:353) 在 org.springframework.data.repository.query。 parser.PartTree.(PartTree.java:84) 在 org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103) 在 org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:63) 在org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:214) at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77) at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.(RepositoryFactorySupport.java:435) at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:220) at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:266) 在 org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:252) 在 org.springframework.data。 jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory。初始化Bean(AbstractAutowireCapableBeanFactory.java:1579)support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:252) at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods( AbstractAutowireCapableBeanFactory.java:1642) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:252) at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods( AbstractAutowireCapableBeanFactory.java:1642) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579) 调用InitMethods(AbstractAutowireCapableBeanFactory.java:1642)在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579) 调用InitMethods(AbstractAutowireCapableBeanFactory.java:1642)

据我了解,这应该开箱即用。似乎存储库被绑定到与所需实现不同的实现。有任何想法吗?

gradle 构建脚本中的相关片段

buildscript {
    ext {
        springBootVersion = "1.4.2.RELEASE"
        verifier_version = "1.0.0.RELEASE"
    }
    repositories {
        maven {url "https://plugins.gradle.org/m2/"}
        maven {url "http://repo.spring.io/plugins-release"}
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.build.gradle:propdeps-plugin:0.0.7") // this enables optional dependencies
        classpath("io.spring.gradle:dependency-management-plugin:0.6.1.RELEASE")
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("se.transmode.gradle:gradle-docker:1.2")
        classpath("com.commercehub:gradle-cucumber-jvm-plugin:0.7")
        classpath("org.ajoberstar:grgit:1.1.0")
        classpath("org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.1-rc3")
        classpath("gradle.plugin.org.detoeuf:swagger-codegen-plugin:1.6.3")
        classpath("org.springframework.cloud:spring-cloud-contract-gradle-plugin:${verifier_version}")
        classpath "net.linguica.gradle:maven-settings-plugin:0.5"
    }
}
...
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Brixton.SR7"
        mavenBom "org.springframework.cloud:spring-cloud-contract-dependencies:${verifier_version}"
        mavenBom 'org.springframework.cloud:spring-cloud-stream-dependencies:Brooklyn.SR1'
    }
}
...
compile(
        "org.springframework.boot:spring-boot-starter-data-jpa",
        'org.springframework.data:spring-data-commons',       
        'org.springframework.cloud:spring-cloud-starter-config',
        'org.springframework.cloud:spring-cloud-starter-eureka',
        'org.springframework.cloud:spring-cloud-starter-sleuth',
        'org.springframework.cloud:spring-cloud-sleuth-zipkin', 
        'com.netflix.hystrix:hystrix-javanica',                 
        'org.springframework.boot:spring-boot-starter-aop',     
        "org.springframework.boot:spring-boot-starter-web",
        "io.swagger:swagger-annotations:1.5.9",
        "com.google.code.gson:gson:2.7",
        "gradle.plugin.org.detoeuf:swagger-codegen-plugin:1.6.3",
        "org.springframework:spring-orm",
        "com.oracle.jdbc:ojdbc7:12.1.0.2",
        'org.springframework.cloud:spring-cloud-stream',
        'org.springframework.cloud:spring-cloud-stream-test-support',
        'org.springframework.cloud:spring-cloud-stream-binder-test',
        "org.springframework.boot:spring-boot-starter-hateoas",
        "com.fasterxml.jackson.module:jackson-module-parameter-names",
        "com.fasterxml.jackson.datatype:jackson-datatype-jdk8",
        "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.6.1",
        "org.hibernate:hibernate-core:5.2.6.Final",
        "org.hibernate:hibernate-envers:5.2.6.Final",
        "org.springframework.data:spring-data-envers:1.0.6.RELEASE"
)

提前致谢。

4

1 回答 1

15

您需要为@EnableJpaRepositories(repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class)您的应用程序类添加一个属性(用 注释的类@SpringBootApplication

如这个答案所示:https ://stackoverflow.com/a/36416266

于 2017-07-27T14:55:28.723 回答