在使用Hibernate或者JPA時,我們經常會使用@Where注解實現查詢過濾,在實體類上、實體屬性上、查詢語句上都有應用。
例如:
@Where(clause = "status != -1")
List<SalarySheetEntity> findAllByCreatedByOrderByIdDesc(String login);
@Where(clause = "status != -1") public class SmsAccountEntity extends AbstractAuditingEntity {}
注意:應用在查詢時需要注意,不可以應用在count上,注解源碼可知:
/** * Where clause to add to the element Entity or target entity of a collection. The clause is written in SQL. * A common use case here is for soft-deletes. * * @author Emmanuel Bernard */ @Target({TYPE, METHOD, FIELD}) @Retention(RUNTIME) public @interface Where { /** * The where-clause predicate. */ String clause(); }