Package org.hibernate.annotations
Annotation Type Where
-
@Target({TYPE,METHOD,FIELD}) @Retention(RUNTIME) public @interface Where
Specifies a restriction written in native SQL to add to the generated SQL when querying an entity or collection.For example,
@Where
could be used to hide entity instances which have been soft-deleted, either for the entity class itself:@Entity @Where(clause = "status <> 'DELETED'") class Document { ... @Enumerated(STRING) Status status; ... }
or, at the level of an association to the entity:
@OneToMany(mappedBy = "owner") @Where(clause = "status <> 'DELETED'") List<Document> documents;
The
WhereJoinTable
annotation lets a restriction be applied to an association table:@ManyToMany @JoinTable(name = "collaborations") @Where(clause = "status <> 'DELETED'") @WhereJoinTable(clause = "status = 'ACTIVE'") List<Document> documents;
By default,
@Where
restrictions declared for an entity are applied when loading a collection of that entity type. This behavior is controlled by:- the annotation member
applyInToManyFetch()
, and - the configuration property "hibernate.use_entity_where_clause_for_collections".
Note that
@Where
restrictions are always applied and cannot be disabled. Nor may they be parameterized. They're therefore much less flexible than filters. - the annotation member
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description boolean
applyInToManyFetch
-
-
-
Element Detail
-
clause
String clause
A predicate, written in native SQL.
-
-
-
applyInToManyFetch
boolean applyInToManyFetch
If this restriction applies to an entity type, should it also be applied when fetching aOneToMany
orManyToOne
association that targets the entity type?By default, the restriction is not applied unless the property "hibernate.use_entity_where_clause_for_collections" is explicitly disabled.
- Returns:
true
if the restriction should be applied even if the configuration property is not enabled- Since:
- 6.2
- Default:
- false
-
-