![](/img/trans.png)
[英]Repository query with a List parameter in Spring Data MongoDB
[英]Spring data repository group by column, list of object with other columns
如何讓Spring存儲庫返回一個自定義的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 實體類
@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 列表沒有被映射。
我最終創建了一個“虛擬”父實體,它的行為有點像一個視圖
@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 接口
@Repository
public interface SomeEntityParentRepository extends JpaRepository<SomeEntityParent, Long> {
}
然后可以在服務中使用
@Autowired
private SomeEntityParentRepository repository;
List<SomeEntityParent> results = repository.findAll();
平面結構現在是一個分層對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.