Annotation Type SQLRestriction


  • @Target({TYPE,METHOD,FIELD})
    @Retention(RUNTIME)
    public @interface SQLRestriction
    Specifies a restriction written in native SQL to add to the generated SQL for entities or collections.

    For example, @SQLRestriction could be used to hide entity instances which have been soft-deleted, either for the entity class itself:

     @Entity
     @SQLRestriction("status <> 'DELETED'")
     class Document {
         ...
         @Enumerated(STRING)
         Status status;
         ...
     }
     

    or, at the level of an association to the entity:

     @OneToMany(mappedBy = "owner")
     @SQLRestriction("status <> 'DELETED'")
     List<Document> documents;
     

    The SQLJoinTableRestriction annotation lets a restriction be applied to an association table:

     @ManyToMany
     @JoinTable(name = "collaborations")
     @SQLRestriction("status <> 'DELETED'")
     @SQLJoinTableRestriction("status = 'ACTIVE'")
     List<Document> documents;
     

    Note that @SQLRestrictions are always applied and cannot be disabled. Nor may they be parameterized. They're therefore much less flexible than filters.

    Since:
    6.3
    See Also:
    Filter, DialectOverride.SQLRestriction, SQLJoinTableRestriction
    • Required Element Summary

      Required Elements 
      Modifier and Type Required Element Description
      String value
      A predicate, written in native SQL.
    • Element Detail

      • value

        String value
        A predicate, written in native SQL.