如何在 spring-data-jpa (2x) + spring-rest-webmvc 中选择 MappingContext? [英] How to choose MappingContext in spring-data-jpa (2x) + spring-rest-webmvc?

查看:38
本文介绍了如何在 spring-data-jpa (2x) + spring-rest-webmvc 中选择 MappingContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a Module A that provides authentication through users, groups and related classes. This module uses org.springframework.data:spring-data-jpa:1.6.0.RELEASE to access this data from a database. Of note might be that Module A uses a custom BaseRepository configured by extending JpaRepositoryFactoryBean, but removing this does not resolve the issue below.

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

A second Module B also has some classes and repositories to manage, unrelated to the Module A classes, again using spring-data-jpa for storage, but connected to a different database. This project exposes it's repositories via REST using org.springframework.data:spring-data-rest-webmvc:2.1.0.RELEASE. Module B uses the classes in module A for authenticating users, but does not manipulate those class instances nor does it store any references.

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

The issue I'm having now is that my module B REST APIs work flawlessly when Module A is not present (or with an older version not yet using spring-data-jpa), but when it is I present it breaks on creating self referential links with the below stacktrace:

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

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

It looks to be talking to the wrong MappingContext in the RepositoryFactoryBeanSupport, even if my org.springframework.data.repository.support.Repositories contains all the repositoryBeanNames from both Module A and Module B.

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

Does anyone know how I can enforce the use of a particular MappingContext, perhaps through my extension of RepositoryRestMvcConfiguration?

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

它已被报告为数据休息项目中的错误:
https://jira.spring.io/browse/DATAREST-312

It's since been reported as a bug on the data-rest project:
https://jira.spring.io/browse/DATAREST-312

推荐答案

今天发生在我身上

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

I was trying to query a specific Entity

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

I fix it creating the repository of that class

你的情况是

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

还进行了使用 jpa 存储库所需的所有配置.看这里

also doing all the needed configuration to use jpa repositories. Look here

希望有所帮助.

这篇关于如何在 spring-data-jpa (2x) + spring-rest-webmvc 中选择 MappingContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆