gpt4 book ai didi

java - JpaRepository 从父级检索子级

转载 作者:行者123 更新时间:2023-12-02 10:11:09 25 4
gpt4 key购买 nike

如何从父进程中检索子进程?

假设我有父类和子类。我想从父方检索子项列表。

这是我的家长类。

+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

关于java - JpaRepository 从父级检索子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54989470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com