[英]CrudRepository Filter By One to Many Relation
我有兩個實體客戶和訂單:
@Entity
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
.....
@OneToMany(mappedBy="customer")
private Set<Order> orders = new HashSet<Order>();
.....
}
@Entity
public class Order {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
........
@ManyToOne()
private Customer;
........
}
在CrudRepository
我想找到所有沒有訂單的客戶。 我該如何編寫@Query? 或者如何在 CrudRepository 接口中編寫方法?
@Repository
public interface CustomerRepository extends CrudRepository<Customer, Long>{
findBy ......
@Query(".......")
find.....
}
謝謝!
要查詢不存在的數據,您必須使用left join
,如下所示:
@Query("select c from Customer as c left join c.orders as orders where orders is null")
您需要使用可與集合表達式一起使用的empty
關鍵字(無需加入)。
JPQL 查詢將是
select c from Customer c where c.orders is empty
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.