Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JPA CRUD存储库查找子实体计数_Java_Spring_Spring Data_Spring Data Jpa_Crud - Fatal编程技术网

Java 使用JPA CRUD存储库查找子实体计数

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

我一直在使用JPA CRUD存储库默认方法,例如find、findAll、delete等,用于我的所有数据库操作

现在我有两个实体:

@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);
}