繁体   English   中英

CrudRepository 按一对多关系过滤

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM