【问题标题】:Using ObjectDB with Spring Data JPA "com.objectdb.jpa.EMF is not an interface"将 ObjectDB 与 Spring Data JPA 一起使用“com.objectdb.jpa.EMF 不是接口”
【发布时间】:2019-06-20 02:36:00
【问题描述】:

繁花似锦

我正在尝试使用 Spring Data JPA(2.1.4,看似最新)获取 ObjectDB(2.7.6_01,最新)。

Spring Data JPA 的文档说需要 2.1 版 JPA 提供程序。 AFAIKT ObjectDB 的 JPA 提供程序是 2.0 ...不确定这是否是问题。

但我的问题是这个例外:

Caused by: java.lang.IllegalArgumentException: com.objectdb.jpa.EMF is not an interface

这是什么原因造成的:

EntityManagerFactory interface [class com.objectdb.jpa.EMF] seems to conflict with Spring's EntityManagerFactoryInfo mixin - consider resetting the 'entityManagerFactoryInterface' property to plain [javax.persistence.EntityManagerFactory]

我很高兴我的代码正确选择了 ObjectDB 实体管理器工厂,但 Spring 围绕此类 (EMF) 的 CGLIB 包装器无法正常工作。

有人有什么想法吗?

Gradle 依赖项:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compile files('libs/objectdb-jee.jar')
    compileOnly 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

然后,这两个 @Bean 中的任何一个(一个或另一个,而不是两者)都会导致上述相同的 EMF 异常:

@Bean
public JpaVendorAdapter jpaVendorAdapter() {

    final ObjectdbJpaVendorAdapter vendorAdapter = new ObjectdbJpaVendorAdapter();

    return vendorAdapter;
}

或者

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

    final ObjectdbJpaVendorAdapter vendorAdapter = new ObjectdbJpaVendorAdapter();

    vendorAdapter.setShowSql(true);
    vendorAdapter.setGenerateDdl(false);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);

    factory.setPackagesToScan("com.example.demo.persistence");
    factory.setDataSource(dataSource());
    factory.afterPropertiesSet();

    return factory;
}

我有一个无操作的 DataSource @Bean 来保持 Spring 的某些方面快乐,但我认为它在这个问题中没有发挥积极作用。

根本没有设置 spring.jpa.*。

干杯

【问题讨论】:

  • 我不知道您问题的完整答案,但我可以推测。您看到的异常是 java.lang.Proxy.getProxyClass 在您将类而不是接口传递给它时抛出的异常。我认为您以某种方式使用 ObjectDB EntityManagerFactory 实现类配置 Spring,而 Spring 正在尝试为此创建代理。错误消息建议您告诉 Spring 代理 EntityManagerFactory 接口。
  • 嗨,威利斯 - 我在破译错误消息方面已经走到了这一步。但我还不知道是否有任何供应商 EntityManagerFactory 打算成为一个接口。最终,它必须是一个类,否则它不会做任何事情。
  • 我还发现了如何更改该界面,就像其中一个例外所暗示的那样。
  • 请尝试按照objectdb.com/forum/2328 上的说明进行操作,或者发布一个失败的完整测试用例以供进一步审查。
  • 感谢您,我已经尝试了该论坛帖子中的几乎所有内容,包括 JpaVendorAdapter bean 等。给我几天时间,我将发布一个完整的非工作设置的 git repo。

标签: spring jpa objectdb


【解决方案1】:

您必须提供的Beans 要简单得多:

@Bean
@ConfigurationProperties("app.datasource")
public DataSource dataSource() {
   return DataSourceBuilder.create().build();
}

@Bean(name="entityManagerFactory")
public EntityManagerFactory getEntityManagerFactoryBean() {
   // this is the important part - here we use a local objectdb file
   // but you can provide connection string to a remote objectdb server
   // in the same way you create objectdb EntityManagerFactory not in Spring
   return Persistence.createEntityManagerFactory("spring-data-jpa-test.odb");
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
   JpaTransactionManager txManager = new JpaTransactionManager();
   txManager.setEntityManagerFactory(emf);
   return txManager;
}

有了上述内容(以及您的pom.xml 中的正确依赖项),就不需要任何额外的配置(即不需要在 application.properties 中进行任何配置)。

可以在here 找到一个简单的工作示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-10
    • 2014-10-21
    • 1970-01-01
    • 2017-08-26
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    相关资源
    最近更新 更多