Annotation Interface Column


@Retention(RUNTIME) @Target(FIELD) public @interface Column
Annotation for customizing field-to-column mapping.

By default, field names are converted to snake_case. Use this annotation to specify a custom column name, mark a field as transient, control nullability, default values, and indexing.

Example:

@Entity
public class UserData {
    private Long id;

    @Column(name = "discord_user_id", nullable = false)
    private Long userId;

    @Column(defaultValue = "true")
    private Boolean enabled;

    @Column(unique = true)
    private String email;

    @Column(index = true)
    private String username;

    @Column(ignore = true)
    private transient String cachedValue;  // Not persisted
}

  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Default value for the column (SQL expression).
    boolean
    Whether this field should be ignored during persistence.
    boolean
    Whether to create a regular index on this column.
    Custom column name.
    boolean
    Whether the column allows NULL values.
    boolean
    Whether to create a unique index on this column.
  • Element Details

    • name

      String name
      Custom column name.

      If empty, the field name is converted to snake_case.

      Returns:
      the column name
      Default:
      ""
    • ignore

      boolean ignore
      Whether this field should be ignored during persistence.
      Returns:
      true to ignore this field
      Default:
      false
    • nullable

      boolean nullable
      Whether the column allows NULL values.

      If not specified, nullability is inferred from the field type: primitive types are non-nullable, wrapper types are nullable.

      Returns:
      true if nullable, false if not nullable, default is inferred
      Default:
      true
    • defaultValue

      String defaultValue
      Default value for the column (SQL expression).

      Example: "true", "0", "'default_text'", "CURRENT_TIMESTAMP"

      Returns:
      the default value expression, or empty string for none
      Default:
      ""
    • unique

      boolean unique
      Whether to create a unique index on this column.
      Returns:
      true to create a unique index
      Default:
      false
    • index

      boolean index
      Whether to create a regular index on this column.
      Returns:
      true to create an index
      Default:
      false