2015-04-04 23 views
9

Giả sử tôi có tổ chức (getters/setters và chi tiết khác nhau bỏ qua cho ngắn gọn):mùa xuân truy vấn dữ liệu mà cột là null

@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; 
} 

Tôi muốn lấy tất cả các phiếu giảm giá cho một khách hàng cho có vô usedOn. tôi, đã không thành công được xác định một phương pháp trong CouponRepository như mô tả trong docs

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

nhưng điều này dẫn vào một lỗi biên dịch Syntax error, insert "... VariableDeclaratorId" to complete FormalParameterList.

Trả lời

7

lỗi của tôi, định nghĩa chính xác là

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

Tôi chỉ đơn giản là bỏ lỡ tên tham số :-(

10

Hãy thử thay đổi phương pháp của bạn này (giả sử Customer.id là một chặng đường dài):

Collection<Coupon> findByCustomer_IdAndUsedOnIsNull(Long customerId);

sau đó sử dụng như thế này:

repo.findByCustomer_IdAndUsedOnIsNull(customer.getId());

Các vấn đề liên quan