Package org.hibernate.annotations
Annotation Interface Generated
Specifies that the value of the annotated property is generated by the
database. The generated value will be automatically retrieved using a
SQL
select after it is generated.
@Generated relieves the program of the need to explicitly call
refresh() to synchronize
state held in memory with state generated by the database when a SQL
insert or update is executed.
This is most useful when:
- a database table has a column value populated by a database trigger,
- a mapped column has a default value defined in DDL, in which case
@Generatedis used in conjunction withColumnDefault, - a SQL expression is used to compute the value of a mapped column, or
- when a custom SQL
insertorupdatestatement specified by an entity assigns a value to the annotated property of the entity, or transforms the value currently assigned to the annotated property.
On the other hand:
- for identity/autoincrement columns mapped to an identifier property,
use
GeneratedValue, and - for columns with a
generated always asclause, prefer theGeneratedColumnannotation, so that Hibernate automatically generates the correct DDL.
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionSpecifies the events that cause the value to be generated by the database.A SQL expression used to generate the value of the column mapped by the annotated property.booleanDetermines if the value currently assigned to the annotated property is included in SQLinsertandupdatestatements.
-
Element Details
-
event
EventType[] eventSpecifies the events that cause the value to be generated by the database.- If
EventType.INSERTis included, the generated value will be selected after each SQLinsertstatement is executed. - If
EventType.UPDATEis included, the generated value will be selected after each SQLupdatestatement is executed.
- Default:
- {INSERT}
- If
-
sql
String sqlA SQL expression used to generate the value of the column mapped by the annotated property. The expression is included in generated SQLinsertandupdatestatements.- Default:
- ""
-
writable
boolean writableDetermines if the value currently assigned to the annotated property is included in SQLinsertandupdatestatements. This is useful if the generated value is obtained by transforming the assigned property value as it is being written.Often used in combination with
SQLInsert,SQLUpdate, orColumnTransformer.write().- Returns:
trueif the current value should be included in SQLinsertandupdatestatements.
- Default:
- false
-