为什么PersistentEntityResourceAssembler:getSingleResourceLinkTo使用实体而不是存储库来构建单个资源链接?

时间:2015-08-23 19:19:10

标签: java spring spring-data-rest

我注意到最新的春季数据发布培训Gosling-RC1发生了一些变化 - 查看我现在可以看到的PersistentEntityResourceAssembler方法getSelfLinkFor现在正在使用entities查找PersistentEntity。更确切地说,就在这里:

https://github.com/spring-projects/spring-data-rest/commit/f8c7a1c376f64b9a36f5bb6bf834458402fd8b09#diff-b5d65266b4181597ee26baed60291de6R195

在使用repositories之前和repositories之内有这个递归调用:

    if (!userType.equals(Object.class)) {
        return getRepositoryFactoryInfoFor(userType.getSuperclass());
    }

检查超类是否Object尝试使用超类。 PersistentEntities类没有这个递归调用:

        if (context.hasPersistentEntityFor(type)) {
            return context.getPersistentEntity(type);
        }

所以现在我得到了不同的行为。我有ProductMyProduct扩展Product。对于Product我有一个存储库,但对于MyProduct我没有。使用Gosling-M1发布列车,我的链接呈现如下:

    "self" : {
      "href" : "http://localhost/rest/product/564546483459328{?projection}",
      "templated" : true
    }

但是当我升级到Gosling-RC1时,它们会像这样呈现:

    "self" : {
      "href" : "http://www.solarapparel.com/rest/myProductModel/564546483459328{?projection}",
      "templated" : true
    }

当我尝试调用http://www.solarapparel.com/rest/myProductModel时,它返回404,因为没有这样的存储库。

这是一个错误吗?或者它是一种已知的行为,这是它应该如何工作的?

1 个答案:

答案 0 :(得分:0)

不,不是,因为你将苹果与橙子进行比较。在查找存储库时,自然也可以查找超类型的存储库,因为存储库自然也可以保留超类型。

在查找特定类型的持久性元数据时,这当然是不同的。想象一下,你正在寻找反射API中特定类型的所有字段,它会继续检查超类型。这显然会产生无效结果。

PersistentEntityResourceAssembler使用PersistentEntities代替Repositories实例的原因是 - 顾名思义 - 它也将用于为实体创建Resource个实例不是聚合根。