![](/img/trans.png)
[英]Not able to fetch child entities using inner join in spring data jpa
[英]Spring Data JPA - how to fetch parents by child id?
我有@ManyToMany注釋的父和子實體:
@Entity
@Table(name = "parent")
public class Parent {
@Id
@GenericGenerator(name = "uuid-gen", strategy = "uuid2")
@GeneratedValue(generator = "uuid-gen",strategy=GenerationType.IDENTITY)
private String id;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "parents_childs",
joinColumns = {@JoinColumn(name = "parent_id", nullable = false, updatable = false)},
inverseJoinColumns = {@JoinColumn(name = "child_id", nullable = false, updatable = false)})
private List<Child> childs;
}
和兒童實體:
@Entity
@Table(name="child")
public class Child {
@Id
@GenericGenerator(name = "uuid-gen", strategy = "uuid2")
@GeneratedValue(generator = "uuid-gen",strategy=GenerationType.IDENTITY)
private String id;
}
我的任務是找到所有包含具有特定ID的Child的父母。 我嘗試以這種方式在我的存儲庫中執行此操作:
@Query("select p from Parent p where p.childs.id = :childId and --some other conditions--")
@RestResource(path = "findByChildId")
Page<Visit> findByChild(@Param("childId") final String childId, final Pageable pageable);
例外:
java.lang.IllegalArgumentException: org.hibernate.QueryException: illegal attempt to dereference collection [parent0_.id.childs] with element property reference [id] [select p from Parent p where p.childs.id = :childId and --some other conditions--]
我知道有可能解決方法名稱如findByChilds_Id
(如此處 )的添加_
但我找不到如何在@Query
注釋中編寫它。
如何使用JPQL編寫它?
我找到了解決方案:
@Query("select p from Parent p join p.childs c where c.id = : childId and --some other conditions--")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.