Spring data query where column is null

lrkwz picture lrkwz · Apr 4, 2015 · Viewed 64k times · Source

Suppose I have entities (getters/setters and various details omitted for brevity):

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

I wish retrieve all Coupons for a given Customer having null usedOn. I,'ve unsuccessfully defined a method in the CouponRepository as described in docs

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

but this leads on a compiler error Syntax error, insert "... VariableDeclaratorId" to complete FormalParameterList.

Answer

lrkwz picture lrkwz · Apr 4, 2015

My fault, the correct definition is

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

I simply missed the parameter name :-(