Package org.hibernate.annotations
Annotation Type View
-
@Incubating @Target(TYPE) @Retention(RUNTIME) public @interface View
Maps an entity to a database view. The name of the view is determined according to the usual rules regarding table mappings, and may be customized using the JPA-standard@Table
annotation. This annotation specifies the query which defines the view, allowing the view to be exported by the schema management tooling.For example, this mapping:
@Immutable @Entity @Table(name="summary") @View(query="select type, sum(amount) as total, avg(amount) as average from details group by type") @Synchronize("details") public class Summary { @Id String type; Double total; Double average; }
results in the following generated DDL:
create view summary as select type, sum(amount) as total, avg(amount) as average from details group by type
If a view is not updatable, we recommend annotating the entity
@Immutable
.It's possible to have an entity class which maps a table, and another entity which maps a view defined as a query against that table. In this case, a stateful session is vulnerable to data aliasing effects, and it is the responsibility of client code to ensure that changes to the first entity are flushed to the database before reading the same data via the second entity. The
@Synchronize
annotation can help alleviate this problem, but it's an incomplete solution.Therefore, we recommend the use of stateless sessions when interacting with entities mapped to views.
- Since:
- 6.3
-
-
Element Detail
-
query
String query
The SQL query that defines the view.
-
-