Java 使用JPA CRUD存储库查找子实体计数
我一直在使用JPA CRUD存储库默认方法,例如find、findAll、delete等,用于我的所有数据库操作 现在我有两个实体:Java 使用JPA CRUD存储库查找子实体计数,java,spring,spring-data,spring-data-jpa,crud,Java,Spring,Spring Data,Spring Data Jpa,Crud,我一直在使用JPA CRUD存储库默认方法,例如find、findAll、delete等,用于我的所有数据库操作 现在我有两个实体: @Entity public class Parent implements Serializable { @Id @GeneratedValue private long id; @OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE) private
@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;
}
试试这些:
public interface ParentRepo extends JpaRepository<Parent, Long> {
Long countByChildren_Parent(Parent parent);
@Query("select count(c) from Parent p join p.children c where p = ?1")
Long countChildrenByParent(Parent parent);
Long countByChildren_ParentId(Long id);
@Query("select count(c) from Parent p join p.children c where p.id = ?1")
Long countChildrenByParentId(Long id);
}
public interface ChildRepo extends JpaRepository<Child, Long> {
Long countByParent(Parent parent);
@Query("select count(c) from Child c where c.parent = ?1")
Long countChildrenByParent(Parent parent);
Long countByParentId(Long id);
@Query("select count(c) from Child c where c.parent.id = ?1")
Long countChildrenByParentId(Long id);
}
公共接口ParentRepo扩展了JpaRepository{
长countByChildren\u Parent(父母);
@查询(“从父项p中选择计数(c)连接p.children c,其中p=?1”)
长countChildrenByParent(父/母);
长countByChildren\u ParentId(长id);
@查询(“从父项p选择计数(c)连接p.children c,其中p.id=?1”)
长countChildrenByParentId(长id);
}
公共接口ChildRepo扩展了JpaRepository{
长countByParent(父-父);
@查询(“从子c中选择计数(c),其中c.parent=?1”)
长countChildrenByParent(父/母);
长countByParentId(长id);
@查询(“从子c中选择计数(c),其中c.parent.id=?1”)
长countChildrenByParentId(长id);
}
试试这些:
public interface ParentRepo extends JpaRepository<Parent, Long> {
Long countByChildren_Parent(Parent parent);
@Query("select count(c) from Parent p join p.children c where p = ?1")
Long countChildrenByParent(Parent parent);
Long countByChildren_ParentId(Long id);
@Query("select count(c) from Parent p join p.children c where p.id = ?1")
Long countChildrenByParentId(Long id);
}
public interface ChildRepo extends JpaRepository<Child, Long> {
Long countByParent(Parent parent);
@Query("select count(c) from Child c where c.parent = ?1")
Long countChildrenByParent(Parent parent);
Long countByParentId(Long id);
@Query("select count(c) from Child c where c.parent.id = ?1")
Long countChildrenByParentId(Long id);
}
公共接口ParentRepo扩展了JpaRepository{
长countByChildren\u Parent(父母);
@查询(“从父项p中选择计数(c)连接p.children c,其中p=?1”)
长countChildrenByParent(父/母);
长countByChildren\u ParentId(长id);
@查询(“从父项p选择计数(c)连接p.children c,其中p.id=?1”)
长countChildrenByParentId(长id);
}
公共接口ChildRepo扩展了JpaRepository{
长countByParent(父-父);
@查询(“从子c中选择计数(c),其中c.parent=?1”)
长countChildrenByParent(父/母);
长countByParentId(长id);
@查询(“从子c中选择计数(c),其中c.parent.id=?1”)
长countChildrenByParentId(长id);
}
别忘了接受/投票给帮助你的答案…别忘了接受/投票给帮助你的答案…我尝试了COUNT
但没有成功。SIZE
正在工作。Thanks我尝试了COUNT
,但没有成功。SIZE
正在工作。谢谢请确认@Query中的计数是否有效。我试过了,但没用。当我使用SIZE
时,它按照下面@xyz的回答工作。请确认COUNT
是否在@Query中工作。我试过了,但没用。当我使用SIZE
时,它按照下面@xyz的答案工作。
public interface ParentRepo extends JpaRepository<Parent, Long> {
Long countByChildren_Parent(Parent parent);
@Query("select count(c) from Parent p join p.children c where p = ?1")
Long countChildrenByParent(Parent parent);
Long countByChildren_ParentId(Long id);
@Query("select count(c) from Parent p join p.children c where p.id = ?1")
Long countChildrenByParentId(Long id);
}
public interface ChildRepo extends JpaRepository<Child, Long> {
Long countByParent(Parent parent);
@Query("select count(c) from Child c where c.parent = ?1")
Long countChildrenByParent(Parent parent);
Long countByParentId(Long id);
@Query("select count(c) from Child c where c.parent.id = ?1")
Long countChildrenByParentId(Long id);
}