Package org.hibernate.annotations

Annotation Interface Generated


@Target({FIELD,METHOD})@Retention(RUNTIME)public @interfaceGenerated
Specifies that the value of the annotated property is generated by the database. The generated value is automatically retrieved after it is generated.

Retrieval of the generated value might require an additional SQLselect statement, but this not necessary if:

  • the SQL dialect of the databasesupportsreturning orselect from final table, or
  • the JDBC driverprovides a similar capability viagetGeneratedKeys().

@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 with the@ColumnDefault annotation,
  • aSQL expression is used to compute the value of a mapped column,
  • a custom SQLinsert orupdate statement specified by an entity assigns a value to the annotated property of the entity, ortransforms the value currently assigned to the annotated property, or
  • there is no mapped column, and the value of the field is determined by evaluating a SQL@Formula.

On the other hand:

  • for identity/autoincrement columns mapped to an identifier property, useGeneratedValue, and
  • for columns with agenerated always as clause, prefer theGeneratedColumn annotation, so that Hibernate automatically generates the correct DDL.

A@Generated field may be generated oninserts, onupdates, or on both inserts and updates, as specified by theevent() member. By default,@Generated fields are not immutable, and so a field which is generated on insert may later be explicitly assigned a new value by the application program, resulting in its value being updated in the database. If this is not desired, the@Immutable annotation may be used in conjunction with@Generated to specify that the field may never be updated after initial generation of its value.

See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Specifies 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.
    boolean
    Determines if the value currently assigned to the annotated property is included in SQLinsert andupdate statements.
  • Element Details

    • event

      EventType[] event
      Specifies the events that cause the value to be generated by the database.
      • IfEventType.INSERT is included, the generated value will be selected after each SQLinsert statement is executed.
      • IfEventType.UPDATE is included, the generated value will be selected after each SQLupdate statement is executed.
      Default:
      {INSERT}
    • 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, or
      • assigning a value disables generation of a value.

      Often used in combination withSQLInsert,SQLUpdate, orColumnTransformer.write().

      Returns:
      true if the current value should be included in SQLinsert andupdate statements.
      Default:
      false