JpaRepository 从 Parent 中检索 Child

JpaRepository Retrieving Child from Parent

如何从 Parent 中检索 Child?

假设我有 parent class 和 child class。我想从 parent 端检索 child 的列表。

这是我的 Parent Class.

+import ...
@Entity

public class Parent {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @OneToMany(mappedBy="parent", cascade = CascadeType.REMOVE, orphanRemoval = true)
    private List<Child> Childs = new ArrayList<>();

    private String name;

    * Getter and Setter are hide
}

这是我的 Parent Child.

+import ...

@Entity
public class Child {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @ManyToOne()
    private Parent parent;

    private String childNote;

    * Getter and Setter are hide
}

这是我的存储库

@Repository
public interface ParentRepository extends JpaRepository<Parent, Long> {

    @Query(value = "SELECT p.childs FROM Parent p where p.id =:id")     
    List<Child> findxx(Long id);
}

这给我一个错误:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-03-05 00:59:16.281 ERROR 444 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testApplication': Unsatisfied dependency expressed through field 'parentRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parentRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.example.test.ParentRepository.findxx(java.lang.Long)!
... I cut-off this line's 

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'parentRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.example.test.ParentRepository.findxx(java.lang.Long)!
... I cut-off this line's 

Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.example.test.ParentRepository.findxx(java.lang.Long)!
... I cut-off this line's 

Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: childs of: com.example.test.Parent [SELECT p.childs FROM com.example.test.Parent p where p.id =:id]
... I cut-off this line's 

Caused by: org.hibernate.QueryException: could not resolve property: childs of: com.example.test.Parent [SELECT p.childs FROM com.example.test.Parent p where p.id =:id]
... I cut-off this line's 

Caused by: org.hibernate.QueryException: could not resolve property: childs of: com.example.test.Parent
    at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:73) ~[hibernate-core-
... I cut-off this line's 

真的需要你的建议。

鸡骨

SELECT c FROM Child c where c.parent.id =:id