为什么Spring Data Rest会维持继承实体的任意顺序

时间:2018-11-07 23:23:44

标签: java spring-data-jpa spring-data spring-data-rest

Spring数据其余部分面临一个挑战,即排序仅适用于实体实现中的实例,不适用于整个结果集。

实体:

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "Entity_Type")
public abstract class BaseEntity implements Identifiable<Long> {
    public Long getId();

    @ManyToOne
    RelatedEntity related;
}

@Entity
@DiscriminatorValue("Entity1")
public class Entity1 extends BaseEntity { }

@Entity
@DiscriminatorValue("Entity2")
public class Entity2 extends BaseEntity { }

存储库:

@NoRepositoryBean
public interface AbstractEntityRepository<T extends BaseEntity> extends PagingAndSortingRepository<T, Long> {
   List<T> findByRelatedEntityId(@Param("id") Long id, Sort sort);
}

@RepositoryRestResource
public interface BaseEntityRepository extends AbstractEntityRepository<BaseEntity> { }

+ repository for each "T extends BaseEntity"

现在出现问题:

指定了baseEntityRepository/search/findByRelatedEntity?id=...&sort=id,asc

搜索方法&sort=id,desc 正确地对实体“组”内的实体进行排序,但实体“组”的顺序为以任意方式呈现。 findAll的任意顺序似乎有所不同,但是如果不知道到底发生了什么,很难确认。输出示例:

示例:

sort = id,asc

{
    "_embedded": {
        "entity1s": [ "id": 1... ],
        "entity2s": [ "id": 3... ],
        "entity3s": [ "id": 2,10,20,... ],   <--- illustrates order is correct within group
        "entity4s": [ "id": 4... ]
   }
}

sort = id,desc

{
    "_embedded": {
        "entity4s": [ "id": 4... ],
        "entity1s": [ "id": 1... ],
        "entity3s": [ "id": 20,10,2... ],  <--- illustrates order is correct within group
        "entity2s": [ "id": 3] 
   }
}

问题:

1-弹簧数据休止符中的“组”如何调用?

2-任意顺序的原因是什么?在第一个示例中,为什么entity3s(最低ID = 2)没有出现在entity2s(最低ID = 3)之前?

3-即使混淆了实体,我也能获得纯粹按ID排序的实体吗?

4-对于findAll,为什么分页输出(前20条记录)和完整输出的结果不同? (&size=1000

0 个答案:

没有答案