jparepository从父母那里检索孩子



如何从父母那里找回孩子?

假设我有家长课和子班。我想从父母身边检索孩子列表。

这是我的父班。

+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
}

这是我的父母。

+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 

确实需要您的建议。

jigu

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

最新更新