我有两个实体Customer和Order:
@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.....
}
谢谢!
答案 0 :(得分:0)
要查询不存在的数据,必须使用left join
,如下所示:
@Query("select c from Customer as c left join c.orders as orders where orders is null")
答案 1 :(得分:0)
您需要使用empty
关键字,该关键字可以与集合表达式一起使用(无需联接)。
JPQL查询应为
select c from Customer c where c.orders is empty