Spring Data Rest 在使用超类型 REST 端点时按子类型组织查询结果
Spring Data Rest is organizing query results by sub-type when when using super-type REST endpoints
给定一个 Spring Data Rest 项目,假设我有以下实体:
@Entity
@Inheritance
@DiscriminatorColumn(name = "blabla")
public abstract class Type1 {
//class code
}
@Entity
public class Type2 extends Type1 {
//class code
}
您可能已经注意到我正在使用 table 继承来模拟我的 Java 继承层次结构。然后我有以下存储库(来自 Spring Data Rest):
public interface Type1Repository extends JpaRepository<Type1, Long> {
}
并且:
public interface Type2Repository extends JpaRepository<Type2, Long> {
}
现在假设我在数据库中有一些 Type2 行。每当我点击整个 Type1 REST 集合(最终调用从 JpaRepository 继承的 Type1Repository 中的 findAll 方法)时,如下所示:
http://{{IP}}:{{PORT}}/type1
我收到一个看起来像这样的数据结构:
{
"_embedded": {
"type2": [
...
]
},
"_links": {
...
},
"page": {
"size": 20,
"totalElements": 206,
"totalPages": 11,
"number": 0
}
}
事情是这样的……我的实体是按子类型组织检索的。所以 Type2 实体的整个页面将在 _embedded.type2 数组中。这在某些情况下很酷,但在我的特定情况下,我只想将它们作为 Type1 实体检索,因为我已经访问了 Type1 集合。更清楚地说,我想要的是以下数据结构:
{
"_embedded": {
"type1": [
...
]
}, ...
如何配置我的 Data Rest 存储库才能以这种方式工作?
注意:
即使在定义我自己的显式查询方法时,我也遇到了这个问题:
public interface Type1Repository extends JpaRepository<Type1, Long> {
@Query(...)
public Page<Type1> someQuery(Pageable pageable);
}
注意:
我正在使用 Spring boot 1.3.2.RELEASE
请帮忙!
我找到了解决办法。它来自 Type2 存储库:
@RepositoryRestResource(collectionResourceRel = "type1")
public interface Type2Repository extends JpaRepository<Type2, Long> {
}
给定一个 Spring Data Rest 项目,假设我有以下实体:
@Entity
@Inheritance
@DiscriminatorColumn(name = "blabla")
public abstract class Type1 {
//class code
}
@Entity
public class Type2 extends Type1 {
//class code
}
您可能已经注意到我正在使用 table 继承来模拟我的 Java 继承层次结构。然后我有以下存储库(来自 Spring Data Rest):
public interface Type1Repository extends JpaRepository<Type1, Long> {
}
并且:
public interface Type2Repository extends JpaRepository<Type2, Long> {
}
现在假设我在数据库中有一些 Type2 行。每当我点击整个 Type1 REST 集合(最终调用从 JpaRepository 继承的 Type1Repository 中的 findAll 方法)时,如下所示:
http://{{IP}}:{{PORT}}/type1
我收到一个看起来像这样的数据结构:
{
"_embedded": {
"type2": [
...
]
},
"_links": {
...
},
"page": {
"size": 20,
"totalElements": 206,
"totalPages": 11,
"number": 0
}
}
事情是这样的……我的实体是按子类型组织检索的。所以 Type2 实体的整个页面将在 _embedded.type2 数组中。这在某些情况下很酷,但在我的特定情况下,我只想将它们作为 Type1 实体检索,因为我已经访问了 Type1 集合。更清楚地说,我想要的是以下数据结构:
{
"_embedded": {
"type1": [
...
]
}, ...
如何配置我的 Data Rest 存储库才能以这种方式工作?
注意: 即使在定义我自己的显式查询方法时,我也遇到了这个问题:
public interface Type1Repository extends JpaRepository<Type1, Long> {
@Query(...)
public Page<Type1> someQuery(Pageable pageable);
}
注意: 我正在使用 Spring boot 1.3.2.RELEASE
请帮忙!
我找到了解决办法。它来自 Type2 存储库:
@RepositoryRestResource(collectionResourceRel = "type1")
public interface Type2Repository extends JpaRepository<Type2, Long> {
}