【发布时间】:2020-08-25 05:30:41
【问题描述】:
我想知道是否有办法做一些事情像 Stream API groupingBy Collector。我知道如何选择一列(问题被标记为重复,我不相信它是)我想做一个分组(像 java Stream API 的 groupingBy 收集器)而不是一个组经过。 假设你有一个表(一个表),比如:
+---------------------+
+ myTable +
+----------+----------+
+ column_A | column_B +
+----------+----------+
+ 1 | 1 +
+ 1 | 2 +
+ 1 | 3 +
+ 2 | 1 +
+----------+----------+
我想要类似的东西
@Entity
@Table("MyTable")
public class MyEntity {
@Column("column_A")
private int a;
@Column("column_B") ?
private List<Integer> b; //normanly this wuould just be int b, butI want a list of all b's that have the same a
}
有一个类似的回购
public interface MyCrudRepo extends CrudRepository<MyEntity,Integer>{
List<MyEntity> findByA();
}
或
public interface MyCrudRepo extends CrudRepository<MyEntity,Integer>{
@Query("SELECT m.a,m.b FROM MyEntity m")
List<MyEntity> customFind();
}
这不一定是最终的样子,任何与此类似的工作都可以。我研究了预测,但所有示例都使用 2 个不同的表。
【问题讨论】:
-
你的问题不清楚你想得到什么。请尝试提供有关问题和预期输出的更多说明
-
对于一个包含 a 和 b 列的表,我想要一个带有 'a' 的对象和一个包含相同 'a' 的所有 'b' 的列表。这类似于如果您使用 @OneToMany 有 2 个表但我只有 1 个表,您会做什么。
标签: java hibernate jpa spring-data