JpaRepository Получение ребенка от родителя - PullRequest
0 голосов
/ 04 марта 2019

Как получить ребенка от родителя?.

Допустим, у меня есть родительский класс и дочерний класс.И я хочу получить список детей со стороны родителей.

Это мой родительский класс.

+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

1 Ответ

0 голосов
/ 04 марта 2019
SELECT c FROM Child c where c.parent.id =:id
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...