Hibernate/JPA中@Where使用時注意


在使用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();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM