gpt4 book ai didi

java - 使用 JPA CRUD 存储库查找子实体的数量

转载 作者:行者123 更新时间:2023-11-29 06:51:53 25 4
gpt4 key购买 nike

我一直在为我的所有数据库操作使用 JPA CRUD 存储库默认方法,例如查找、查找全部、删除等。

现在我有两个实体:

@Entity
public class Parent implements Serializable {

@Id
@GeneratedValue
private long id;

@OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE)
private Set<Child> children;
}

@Entity
public class Child implements Serializable {

@Id
@GeneratedValue
private long id;

@ManyToOne
@JoinColumn
private Parent parent;
}

有没有办法让我在 ParentRepository 中创建一个新方法,让我可以根据 parent 的 ID 检索 parent 的所有 child 的计数?

所以在我的 ParentRepository 中,我可以创建一个看起来像这样的方法吗:

int findAllChildrenCount(Long parentID);

最佳答案

@Query("select size(u.children) from Parent u where u.id=:parentID")
int findAllChildrenCount(@Param("parentID")Long parentID);

关于java - 使用 JPA CRUD 存储库查找子实体的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45333978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com