给定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
请帮忙!
答案 0 :(得分:0)
我找到了解决方案。它来自Type2存储库:
@RepositoryRestResource(collectionResourceRel = "type1")
public interface Type2Repository extends JpaRepository<Type2, Long> {
}