【发布时间】:2016-11-13 20:05:02
【问题描述】:
给定一个 Spring Data Rest 项目,假设我有以下实体:
@Entity
@Inheritance
@DiscriminatorColumn(name = "blabla")
public abstract class Type1 {
//class code
}
@Entity
public class Type2 extends Type1 {
//class code
}
您可能已经注意到,我正在使用表继承来为我的 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
请帮忙!
【问题讨论】:
标签: spring-data spring-data-rest