【问题标题】:How to choose MappingContext in spring-data-jpa (2x) + spring-rest-webmvc?spring-data-jpa (2x) + spring-rest-webmvc中如何选择MappingContext?
【发布时间】:2014-07-08 22:31:31
【问题描述】:

我有一个模块 A,它通过用户、组和相关类提供身份验证。该模块使用org.springframework.data:spring-data-jpa:1.6.0.RELEASE 从数据库中访问这些数据。值得注意的是,模块 A 使用了通过扩展 JpaRepositoryFactoryBean 配置的自定义 BaseRepository,但删除它并不能解决以下问题。

第二个模块 B 也有一些类和存储库要管理,与模块 A 类无关,再次使用 spring-data-jpa 进行存储,但连接到不同的数据库。该项目使用 org.springframework.data:spring-data-rest-webmvc:2.1.0.RELEASE 通过 REST 公开其存储库。模块 B 使用模块 A 中的类对用户进行身份验证,但不操作这些类实例,也不存储任何引用。

我现在遇到的问题是,当模块 A 不存在(或旧版本尚未使用 spring-data-jpa)时,我的模块 B REST API 可以正常工作,但是当它出现时,它会中断使用以下堆栈跟踪创建自引用链接:

java.lang.IllegalArgumentException: Cannot create self link for class Document! No persistent entity found!
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.getSelfLinkFor(PersistentEntityResourceAssembler.java:81) ~[spring-data-rest-webmvc-2.1.0.M1.jar:na]
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:64) ~[spring-data-rest-webmvc-2.1.0.M1.jar:na]
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:32) ~[spring-data-rest-webmvc-2.1.0.M1.jar:na]
at org.springframework.data.web.PagedResourcesAssembler.createResource(PagedResourcesAssembler.java:144) ~[spring-data-commons-1.8.0.M1.jar:na]
at org.springframework.data.web.PagedResourcesAssembler.toResource(PagedResourcesAssembler.java:96) ~[spring-data-commons-1.8.0.M1.jar:na]
at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.entitiesToResources(AbstractRepositoryRestController.java:220) ~[spring-data-rest-webmvc-2.1.0.M1.jar:na]
at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.resultToResources(AbstractRepositoryRestController.java:207) ~[spring-data-rest-webmvc-2.1.0.M1.jar:na]
at org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(RepositoryEntityController.java:135) ~[spring-data-rest-webmvc-2.1.0.M1.jar:na]

另请参阅:https://github.com/spring-projects/spring-data-rest/blob/master/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/PersistentEntityResourceAssembler.java#L80

即使我的org.springframework.data.repository.support.Repositories 包含来自模块 A 和模块 B 的所有 repositoryBeanNames,它似乎在与 RepositoryFactoryBeanSupport 中的错误 MappingContext 对话。

有谁知道我可以如何强制使用特定的 MappingContext,也许是通过我的 RepositoryRestMvcConfiguration 扩展?

** 编辑 **
这是一个说明问题的 GitHub 存储库:
https://github.com/timtebeek/dual-data-jpa-rest-webmvc

它已被报告为 data-rest 项目中的一个错误:
https://jira.spring.io/browse/DATAREST-312

【问题讨论】:

  • 你还在 2.1.0.RELEASE 中看到这个吗?
  • 哇,嗯,我会检查的;我使用的是 Spring 4,它与 2.1.0.RELEASE 版本有一些冲突,所以我需要切换回 3.2.x 来稍微测试一下。一旦我有一个工作版本,我会立即更新。
  • @OliverGierke 我可以确认 spring-data-jpa:1.6.0.RELEASE 和 spring-data-rest-webmvc:2.1.0.RELEASE 也会出现这个问题。我可以向您提供任何其他信息来帮助调试吗?
  • 如果不了解更多关于您的模块如何相互交互、它们如何引导、ApplicationContext 结构是什么样的等信息,很难判断。任何机会你都可以想出一个非常精简版应用以重现错误?
  • 感谢您考虑这一点;我会尝试在 github 上创建一些内容并在完成后发布一个链接,但是在不泄露任何公司代码的情况下复制可能需要一小段时间。 :)

标签: spring-data spring-data-jpa spring-data-rest


【解决方案1】:

今天发生在我身上

我试图查询一个特定的实体

我修复它创建该类的存储库

在你的情况下,它会是

@Repository
public interface DocumentRepository extends JpaRepository<Document, Long> {
}

还进行所有需要的配置以使用 jpa 存储库。 Look here

希望能帮到你。

【讨论】:

猜你喜欢
  • 2016-10-26
  • 1970-01-01
  • 2015-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
  • 2023-02-03
相关资源
最近更新 更多