首页 > 解决方案 > Spring数据存储库按列分组,包含其他列的对象列表

问题描述

如何让 Spring 存储库返回一个自定义 DTS,该 DTS 按表中的列 businessValue1 分组,并具有 DTO 对象列表和表中的其他列

我要从中返回 DTS 的表

id | businessValue1 | businessValue2 | businessValue3
1  | "x"            | "uigaiun"      | "guthgi"
2  | "x"            | "rktjuhngit"   | "ujgthniuertn"
3  | "x"            | "nguitren"     | "ikljugnbe"
4  | "y"            | "iughnuitn"    | "eiubgnuie"
5  | "q"            | "rtiluhn"      | "iljughbl"
6  | "q"            | "tkiruln"      | "jutgnhet"

表示表的 Java Entity 类

@Data
@AllArgsConstructor
@Entity
public class SomeEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @NotNull
  private long id;
  
  @NotNull
  private String businessValue1;
  
  @NotNull
  private String businessValue2;
  
  @NotNull
  private String businessValue3;
}

目标类:

DTO 表示表中的其他列

@Data
@AllArgsConstructor
public class SomeDTO {

  private String businessValue2;
  
  private String businessValue3;
}

DTS 表示 businessValue1 列的分组依据,其他列作为 DTO 列表

@Data
@AllArgsConstructor
public class SomeDTS {

  private String businessValue1;
  
  private List<SomeDTO> dtos;
}

我的预期结果将具有如下结构

├── "x"
|    ├── "uigaiun", "guthgi"
|    ├── "rktjuhngit", "ujgthniuertn"
|    └── "nguitren", "ikljugnbe"
|
├── "y"
|    └── "iughnuitn", "eiubgnuie"
|
└── "q"
     ├── "rtiluhn", "iljughbl"
     └── "tkiruln", "jutgnhet"

我认为 Spring Data Repository 接口应该如下所示

@Repository
public interface SomeEntityRepository extends JpaRepository<SomeEntity, Long> {

  @Query(
      "SELECT "
      +  "new SomeDTS "
      +"FROM "
      +  "SomeEntity s "
      +"GROUP BY "
      +  "s.businessValue1")
  public List<SomeDTS> SomeEntityGroupByBusinessValue1();

}

通过我正在努力进行 JPQL 查询,有人可以帮助我吗?

更新 1 - 2020 年 11 月 19 日:

我想我更接近了,来自@Zorglube 的评论

@Repository
public interface SomeEntityRepository extends JpaRepository<SomeEntity, Long> {

  @Query(value =
      "SELECT "+
          " DISTINCT SomeDTS.businessValue1, SomeDTO" +
          " FROM SomeEntity AS SomeDTS" +
          " JOIN SomeEntity AS SomeDTO ON SomeDTS.businessValue1 = SomeDTO.businessValue1")
  Stream<SomeDTS> SomeEntityGroupByBusinessValue1();

}

这仍然给我映射问题

No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [SomeDts]
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [SomeDts]
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174)
    at org.springframework.data.repository.query.ResultProcessor$ProjectingConverter.convert(ResultProcessor.java:297)
    at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.lambda$and$0(ResultProcessor.java:217)
    at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:228)
    at org.springframework.data.repository.query.ResultProcessor.lambda$processResult$0(ResultProcessor.java:163)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
    at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
    at org.hibernate.query.spi.StreamDecorator.collect(StreamDecorator.java:211)    
.
.
.

那么如何进行映射呢?

更新 2 改为切换到接口

@Repository
public interface SomeEntityRepository extends JpaRepository<SomeEntity, Long> {

  @Query(value =
      "SELECT "+
          " DISTINCT SomeDTSIF.businessValue1 as businessValue1, SomeDTOIF" +
          " FROM SomeEntity AS SomeDTSIF" +
          " JOIN SomeEntity AS SomeDTOIF ON SomeDTSIF.businessValue1 = SomeDTO.businessValue1")
  Stream<SomeDTSIF> SomeEntityGroupByBusinessValue1();

}

解决“找不到能够转换的转换器”问题,但我得到了 6 个对象:

├── "x"
|    └── NULL
|
├── "x"
|    └── NULL
|
├── "x"
|    └── NULL
|
├── "y"
|    └── NULL
|
├── "q"
|    └── NULL
|
└── "q"
     └── NULL

并且 SomeDTSIF 中的 SomeDTOIF 列表没有被映射。

标签: javajpaspring-datajpqlspring-repositories

解决方案


我最终制作了一个“虚拟”父实体,它的行为有点像视图

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
@EqualsAndHashCode
@Immutable
@Subselect(
    "SELECT"
        + " DISTINCT SomeEntityParent.currency_source "
        + " FROM some_entity SomeEntityParent"
        + " JOIN some_entity SomeEntity"
        + " ON SomeEntityParent.businessValue1 = SomeEntityParent.businessValue1"
)
public class SomeEntityParent {

  private String businessValue1;
  
  private List<SomeEntity> someEntity;
}

然后是基于它的存储库接口

@Repository
public interface SomeEntityParentRepository extends JpaRepository<SomeEntityParent, Long> {

}

然后可以在服务中使用

@Autowired
private SomeEntityParentRepository repository;
  
List<SomeEntityParent> results = repository.findAll();

平面结构现在是一个分层对象。


推荐阅读