列为空的 Spring 数据查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29448282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:29:23  来源:igfitidea点击:

Spring data query where column is null

springspring-dataspring-data-jpa

提问by lrkwz

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

假设我有实体(为简洁起见,省略了 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;
}

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

我希望检索具有 null usedOn 的给定客户的所有优惠券。我没有成功地在 CouponRepository 中定义了一个方法,如文档中所述

@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.

但这会导致编译器错误Syntax error, insert "... VariableDeclaratorId" to complete FormalParameterList

采纳答案by lrkwz

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 :-(

我只是错过了参数名称:-(

回答by Xorty

Try changing your method to this (assuming Customer.idis a long):

尝试将您的方法更改为此(假设Customer.id很长):

Collection<Coupon> findByCustomer_IdAndUsedOnIsNull(Long customerId);

Collection<Coupon> findByCustomer_IdAndUsedOnIsNull(Long customerId);

then use like this:

然后像这样使用:

repo.findByCustomer_IdAndUsedOnIsNull(customer.getId());

repo.findByCustomer_IdAndUsedOnIsNull(customer.getId());

回答by Nikunj Undhad

You can use IsNullto check null columns in JPA query.

您可以使用IsNull检查 JPA 查询中的空列。

For example for any columnAyou can write query like query like

例如,对于任何columnA,您都可以编写类似查询的查询

findByColumnAIsNull

In this case you can write queries like

在这种情况下,您可以编写如下查询

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

Also you can check how this queries will be enter image description hereRefer this Spring Data JPA Query creation this will help you lot to understand and create different type of JPA query variation.

您还可以检查此查询将如何 在此处输入图片说明参考此 Spring Data JPA 查询创建,这将帮助您了解和创建不同类型的 JPA 查询变体。

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation

https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation