使用 ID 获取 JpaRepository 中的所有 @ManyToOne 关系
Get all @ManyToOne relations in JpaRepository with Ids
我需要从单个存储库方法中获取 List<List<Office>>
。我最终使用了这个方法:
List<List<Office>> lists = officeDao.findAllByCompanyIdIn(ids);
有效,除了查看 lists.get(i)
时,元素多于需要。这是因为如果公司的办公室具有相同的公司 ID,它不会将它们组合在一起(在该级别的同一索引中)。
以下是某些上下文的相关实体:
@Setter
@Getter
@Entity
@AllArgsConstructor
public class Company implements Serializable {
private static final long serialVersionUID = -6007975840330441233L;
@Id
@GeneratedValue
@Column(columnDefinition = "uuid", updatable = false)
private UUID id;
private String name;
private BigDecimal balance;
private CompanyType type;
@OneToMany(
targetEntity = Office.class,
orphanRemoval = true,
fetch = FetchType.LAZY,
mappedBy = "company",
cascade = CascadeType.ALL)
private List<Office> offices;
public enum CompanyType {
PRIVATE_LIMITED,
SOLE_TRADER,
PUBLIC
}
}
@Setter
@Getter
@Entity
@AllArgsConstructor
public class Office implements Serializable {
private static final long serialVersionUID = 476906690321337185L;
@Id
@GeneratedValue
@Column(columnDefinition = "uuid", updatable = false)
private UUID id;
private String officeLocation;
private int maximumHeadcount;
private FinanceType financeType;
@ManyToOne(targetEntity = Company.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Company company;
public enum FinanceType {
RENTING,
OWNED,
LEASING
}
}
对于超级模棱两可的问题深表歉意,但如果您需要更多上下文,请发表评论!
伙计们干杯!
事实证明,您可以使用 Java Streams API(Java 8+)中的一些技术实现我想要的东西。
我不需要在 Entity/Dao 方面进行任何更改,我只是在此处使用此代码块加载它:
List<List<Office>> bucketedList = new ArrayList<>();
officeDao.findAllByCompanyIdIn(ids).stream()
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(office -> office.getCompany().getId()))
.forEach((uuid, offices) -> bucketedList.add(offices));
我实质上是在创建一个新的(嵌套的)列表,其中每个对象将被分类,按 office.getCompany().getId()
分组。
如果大家对此方法有什么建议或者改进,欢迎在下方留言!
我需要从单个存储库方法中获取 List<List<Office>>
。我最终使用了这个方法:
List<List<Office>> lists = officeDao.findAllByCompanyIdIn(ids);
有效,除了查看 lists.get(i)
时,元素多于需要。这是因为如果公司的办公室具有相同的公司 ID,它不会将它们组合在一起(在该级别的同一索引中)。
以下是某些上下文的相关实体:
@Setter
@Getter
@Entity
@AllArgsConstructor
public class Company implements Serializable {
private static final long serialVersionUID = -6007975840330441233L;
@Id
@GeneratedValue
@Column(columnDefinition = "uuid", updatable = false)
private UUID id;
private String name;
private BigDecimal balance;
private CompanyType type;
@OneToMany(
targetEntity = Office.class,
orphanRemoval = true,
fetch = FetchType.LAZY,
mappedBy = "company",
cascade = CascadeType.ALL)
private List<Office> offices;
public enum CompanyType {
PRIVATE_LIMITED,
SOLE_TRADER,
PUBLIC
}
}
@Setter
@Getter
@Entity
@AllArgsConstructor
public class Office implements Serializable {
private static final long serialVersionUID = 476906690321337185L;
@Id
@GeneratedValue
@Column(columnDefinition = "uuid", updatable = false)
private UUID id;
private String officeLocation;
private int maximumHeadcount;
private FinanceType financeType;
@ManyToOne(targetEntity = Company.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Company company;
public enum FinanceType {
RENTING,
OWNED,
LEASING
}
}
对于超级模棱两可的问题深表歉意,但如果您需要更多上下文,请发表评论!
伙计们干杯!
事实证明,您可以使用 Java Streams API(Java 8+)中的一些技术实现我想要的东西。
我不需要在 Entity/Dao 方面进行任何更改,我只是在此处使用此代码块加载它:
List<List<Office>> bucketedList = new ArrayList<>();
officeDao.findAllByCompanyIdIn(ids).stream()
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(office -> office.getCompany().getId()))
.forEach((uuid, offices) -> bucketedList.add(offices));
我实质上是在创建一个新的(嵌套的)列表,其中每个对象将被分类,按 office.getCompany().getId()
分组。
如果大家对此方法有什么建议或者改进,欢迎在下方留言!