Class NativeGenerator

java.lang.Object
org.hibernate.id.NativeGenerator
All Implemented Interfaces:
Serializable, ExportableProducer, AnnotationBasedGenerator<NativeGenerator>, BeforeExecutionGenerator, Generator, OnExecutionGenerator, Configurable

Generator that picks a strategy based on the dialect.
Since:
7.0
See Also:
  • Constructor Details

    • NativeGenerator

      public NativeGenerator()
  • Method Details

    • getGenerationType

      public GenerationType getGenerationType()
    • getEventTypes

      public EnumSet<EventType> getEventTypes()
      Description copied from interface: Generator
      The event types for which this generator should be called to produce a new value.

      Identifier generators must return EventTypeSets.INSERT_ONLY.

      Specified by:
      getEventTypes in interface Generator
      Returns:
      a set of EventTypes.
    • generatedOnExecution

      public boolean generatedOnExecution()
      Description copied from interface: Generator
      Determines if the property value is generated when a row is written to the database, or in Java code that executes before the row is written.
      • Generators which only implement BeforeExecutionGenerator must result false.
      • Generators which only implement OnExecutionGenerator must result true.
      • Generators which implement both subinterfaces may decide at runtime what value to return.
      Specified by:
      generatedOnExecution in interface BeforeExecutionGenerator
      Specified by:
      generatedOnExecution in interface Generator
      Specified by:
      generatedOnExecution in interface OnExecutionGenerator
      Returns:
      true if the value is generated by the database as a side effect of the execution of an insert or update statement, or false if it is generated in Java code before the statement is executed via JDBC.
    • initialize

      public void initialize(NativeGenerator annotation, Member member, GeneratorCreationContext context)
      Description copied from interface: AnnotationBasedGenerator
      Initializes this generation strategy for the given annotation instance.
      Specified by:
      initialize in interface AnnotationBasedGenerator<NativeGenerator>
      Parameters:
      annotation - an instance of the strategy's annotation type. Typically, implementations will retrieve the annotation's attribute values and store them in fields.
      member - the Java member annotated with the generator annotation.
      context - a GeneratorCreationContext
    • configure

      public void configure(GeneratorCreationContext creationContext, Properties parameters)
      Description copied from interface: Configurable
      Configure this instance, given the value of parameters specified by the user as XML <param> elements and @Parameter annotations.

      This method is called just once, following instantiation. If this instance also implements ExportableProducer, then this method is always called before ExportableProducer.registerExportables(Database),

      Specified by:
      configure in interface Configurable
      Parameters:
      creationContext - Access to the generator creation context
      parameters - param values, keyed by parameter name
    • registerExportables

      public void registerExportables(Database database)
      Description copied from interface: ExportableProducer
      Register the contained exportable things to the Database
      Specified by:
      registerExportables in interface ExportableProducer
      Parameters:
      database - The database instance
    • initialize

      public void initialize(SqlStringGenerationContext context)
      Description copied from interface: Configurable
      Initializes this instance, pre-generating SQL if necessary.

      If this instance also implements ExportableProducer, then this method is always called after ExportableProducer.registerExportables(Database), and before first use.

      Specified by:
      initialize in interface Configurable
      Parameters:
      context - A context to help generate SQL strings
    • generate

      public Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType)
      Description copied from interface: BeforeExecutionGenerator
      Generate a value.
      Specified by:
      generate in interface BeforeExecutionGenerator
      Parameters:
      session - The session from which the request originates.
      owner - The instance of the object owning the attribute for which we are generating a value.
      currentValue - The current value assigned to the property, or null
      eventType - The type of event that has triggered generation of a new value
      Returns:
      The generated value
    • referenceColumnsInSql

      public boolean referenceColumnsInSql(Dialect dialect)
      Description copied from interface: OnExecutionGenerator
      Determines if the columns whose values are generated are included in the column list of the SQL insert or update statement. For example, this method should return:
      Specified by:
      referenceColumnsInSql in interface OnExecutionGenerator
      Returns:
      true if the column is included in the column list of the SQL statement.
    • writePropertyValue

      public boolean writePropertyValue()
      Description copied from interface: OnExecutionGenerator
      Determines if the property values are written to JDBC as the argument of a JDBC ? parameter.
      Specified by:
      writePropertyValue in interface OnExecutionGenerator
    • getReferencedColumnValues

      public String[] getReferencedColumnValues(Dialect dialect)
      Description copied from interface: OnExecutionGenerator
      A SQL expression indicating how to calculate the generated values when the mapped columns are included in the SQL statement. The SQL expressions might be:
      • function calls like current_timestamp or nextval('mysequence'), or
      • syntactic markers like default.
      Specified by:
      getReferencedColumnValues in interface OnExecutionGenerator
      Parameters:
      dialect - The SQL dialect, allowing generation of an expression in dialect-specific SQL.
      Returns:
      The column value to be used in the generated SQL statement.
    • getGeneratedIdentifierDelegate

      public InsertGeneratedIdentifierDelegate getGeneratedIdentifierDelegate(EntityPersister persister)
      Description copied from interface: OnExecutionGenerator
      The InsertGeneratedIdentifierDelegate used to retrieve the generated value if this object is an identifier generator.

      This is ignored by GeneratedValuesProcessor, which handles multiple generators at once. So if this object is not an identifier generator, this method is never called.

      Note that this method arguably breaks the separation of concerns between the generator and coordinating code, by specifying how the generated value should be retrieved.

      The problem solved here is: we want to obtain an insert-generated primary key. But, sadly, without already knowing the primary key, there's no completely-generic way to locate the just-inserted row to obtain it.

      We need one of the following things:

      Alternatively, if the generated id is an identity/"autoincrement" column, we can take advantage of special platform-specific functionality to retrieve it. Taking advantage of the specialness of identity columns is the job of one particular implementation: IdentityGenerator. And the need for customized behavior for identity columns is the reason why this layer-breaking method exists.

      Specified by:
      getGeneratedIdentifierDelegate in interface OnExecutionGenerator