我有两个实体:
@Entity
public class Customer {
@Id
@GeneratedValue
private int id;
@Column(nullable = false)
private String name;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Collection<Coupon> coupons;
}
@Entity
public class Coupon {
@Id
@GeneratedValue
private int id;
@Column(nullable = false)
private String title;
@ManyToMany(mappedBy = "coupons")
private Collection<Customer> customers;
}数据库中有三个表:customer,coupon和customer_coupons,客户可以购买优惠券,在购买优惠券后,customer_coupons保存customers_id和coupons_id (客户可以有多个优惠券,优惠券可以有多个客户)。
我需要以某种方式从customer_coupons表中获得customerId和couponId客户的优惠券。我有CouponRepository接口:
@Repository
public interface CouponRepository extends JpaRepository<Coupon, Integer> {
@Query("SELECT c FROM Coupon c WHERE c.id IN (SELECT coupons.id FROM customer_coupons WHERE coupons_id = ?1 AND customer_id = ?2)")
Coupon findCustomerCoupon(int couponId, int customerId);
}但是这个查询不起作用,我得到的是QuerySyntaxException: customer_coupons is not mapped。有人能帮我创建正确的查询吗?
发布于 2019-03-21 04:20:06
从Customer c加入c.coupons优惠券中选择优惠券,其中c.id = :customerId和coupon.id =:优惠券AND
JB Nizet的回答
https://stackoverflow.com/questions/55191275
复制相似问题