2015-04-04 41 views
9

假設我有實體(getter/setter方法,並略去了各種細節):春數據查詢,其中列爲空

@Entity 
class Customer{ 
    ... 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "customer") 
    Collection<Coupon> coupons; 
} 

@Entity 
class Coupon{ 
    ... 
    @Temporal(value = TemporalType.TIMESTAMP) 
    private Date usedOn; 

    @ManyToOne(fetch = FetchType.LAZY) 
    @NotNull 
    Customer customer; 
} 

我希望檢索所有優惠券具有空usedOn一個給定的客戶。 I,'已經如在docs

@Repository 
public interface CouponRepository extends CrudRepository<Coupon, Long> { 
    Collection<Coupon> findByCustomerAndUsedOnIsNull(Customer); 
} 

描述,但是這將導致在一個編譯錯誤Syntax error, insert "... VariableDeclaratorId" to complete FormalParameterList在CouponRepository不成功定義的方法。

回答

7

我的錯,正確的定義是

@Repository 
public interface CouponRepository extends CrudRepository<Coupon, Long> { 
    Collection<Coupon> findByCustomerAndUsedOnIsNull(Customer customer); 
} 

我只是錯過了參數名稱:-(

10

試着改變你的方法(假設Customer.id是一個長期的):

Collection<Coupon> findByCustomer_IdAndUsedOnIsNull(Long customerId);

然後使用這樣的:

repo.findByCustomer_IdAndUsedOnIsNull(customer.getId());