2015-04-04 28 views
9

Załóżmy, że mam podmiotów (pobierające/ustawiające i różne szczegóły pominięte dla zwięzłości):Wiosna kwerendy danych, gdzie kolumna jest 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; 
} 

Chciałbym odzyskać wszystkie kupony dla danego klienta mającego zerowy usedOn. I 've bezskutecznie zdefiniowane metody w CouponRepository jak opisano w docs

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

ale prowadzi to na błąd kompilatora Syntax error, insert "... VariableDeclaratorId" to complete FormalParameterList.

Odpowiedz

7

Moja wina, poprawna definicja jest

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

Po prostu przegapiłem nazwę parametru :-(

10

Spróbuj zmienić swój sposób na to (zakładając Customer.id jest długi):

Collection<Coupon> findByCustomer_IdAndUsedOnIsNull(Long customerId);

następnie używać tak:

repo.findByCustomer_IdAndUsedOnIsNull(customer.getId());