Package org.hibernate.annotations
Annotation Type Generated
-
@Target({FIELD,METHOD}) @Retention(RUNTIME) public @interface Generated
Specifies that the value of the annotated property is generated by the database. The generated value will be automatically retrieved using a SQLselect
after it is generated.@Generated
relieves the program of the need to explicitly callrefresh()
to synchronize state held in memory with state generated by the database when a SQLinsert
orupdate
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
@Generated
is used in conjunction withColumnDefault
, - a SQL expression is used to compute the value of a mapped column, or
- when a custom SQL
insert
orupdate
statement 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 as
clause, prefer theGeneratedColumn
annotation, so that Hibernate automatically generates the correct DDL.
- See Also:
GeneratedValue
,ColumnDefault
,GeneratedColumn
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description EventType[]
event
Specifies the events that cause the value to be generated by the database.String
sql
A SQL expression used to generate the value of the column mapped by the annotated property.GenerationTime
value
Deprecated.useevent()
boolean
writable
Determines if the value currently assigned to the annotated property is included in SQLinsert
andupdate
statements.
-
-
-
Element Detail
-
event
EventType[] event
Specifies the events that cause the value to be generated by the database.- If
EventType.INSERT
is included, the generated value will be selected after each SQLinsert
statement is executed. - If
EventType.UPDATE
is included, the generated value will be selected after each SQLupdate
statement is executed.
- Default:
- {org.hibernate.generator.EventType.INSERT}
- If
-
-
-
value
@Deprecated(since="6.2") GenerationTime value
Deprecated.useevent()
Specifies the events that cause the value to be generated by the database.- If
GenerationTime.INSERT
, the generated value will be selected after each SQLinsert
statement is executed. - If
GenerationTime.UPDATE
, the generated value will be selected after each SQLupdate
statement is executed. - If
GenerationTime.ALWAYS
, the generated value will be selected after each SQLinsert
orupdate
statement is executed.
- Default:
- org.hibernate.annotations.GenerationTime.INSERT
- If
-
-
-
sql
String sql
A SQL expression used to generate the value of the column mapped by the annotated property. The expression is included in generated SQLinsert
andupdate
statements.- Default:
- ""
-
-
-
writable
boolean writable
Determines if the value currently assigned to the annotated property is included in SQLinsert
andupdate
statements. 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:
true
if the current value should be included in SQLinsert
andupdate
statements.
- Default:
- false
-
-