Home/Learn/Hibernate & JPA/@Column Mapping

@Column Mapping

Beginner
Fundamentals

@Column maps a field to a specific column with custom name, length, precision, nullable, and unique constraints that are honoured during schema generation and validation.

Overview

@Column is a JPA annotation that controls the physical column a field maps to. Without it, Hibernate uses the field name as the column name (with an underscore naming strategy if spring.jpa.hibernate.ddl-auto and the ImplicitNamingStrategy are configured). Key attributes include name (override column name), nullable (DDL NOT NULL constraint), length (VARCHAR length), precision and scale (DECIMAL), unique (UNIQUE constraint), insertable/updatable (exclude from INSERT or UPDATE), and columnDefinition (raw SQL type override for DB-specific types). These attributes inform both schema generation (hibernate.hbm2ddl.auto=create) and validation (validate mode).

Common @Column Attributes

The most-used attributes are name, nullable, length, and unique. Precision and scale are essential for DECIMAL/NUMERIC columns storing money. columnDefinition provides an escape hatch for DB-specific types.

Java — @Column attribute examples
@Entity
@Table(name = "products")
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "product_name", nullable = false, length = 200)
    private String name;

    @Column(nullable = false, precision = 10, scale = 2)
    private BigDecimal price;               // maps to DECIMAL(10, 2)

    @Column(name = "sku_code", unique = true, length = 50)
    private String sku;

    @Column(columnDefinition = "TEXT")      // raw DDL type override
    private String description;

    @Column(updatable = false)              // set on insert, never updated
    private LocalDateTime createdAt;
}

insertable and updatable

insertable=false excludes the column from INSERT statements (useful for columns managed by DB defaults). updatable=false locks the value after initial insert — useful for audit timestamps or immutable identifiers.

Java — insertable=false and updatable=false
@Entity
public class Order {

    @Column(name = "created_at", nullable = false, updatable = false,
            columnDefinition = "DATETIME DEFAULT CURRENT_TIMESTAMP")
    private LocalDateTime createdAt;         // set by DB on INSERT, never updated by JPA

    @Column(name = "external_ref", insertable = false, updatable = false)
    private String externalRef;              // managed by a trigger or another system

    @PrePersist
    void prePersist() {
        if (createdAt == null) createdAt = LocalDateTime.now();
    }
}

Naming Strategy and Validation

Spring Boot's default ImplicitNamingStrategy converts camelCase field names to snake_case column names. Use spring.jpa.hibernate.ddl-auto=validate to assert that your @Column definitions match the actual DB schema at startup.

Properties — naming strategy and schema validation
# application.properties

# Naming strategy: camelCase → snake_case (Spring Boot default)
spring.jpa.hibernate.naming.physical-strategy=\
  org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy

# Validate schema at startup — throws if columns are missing or have wrong types
spring.jpa.hibernate.ddl-auto=validate

# Do NOT use create/create-drop in production — use Flyway or Liquibase
# spring.jpa.hibernate.ddl-auto=create   ← drops and recreates tables

Key Points to Remember

  • 1@Column without attributes uses field name (with configured naming strategy) as column name
  • 2nullable=false generates NOT NULL in DDL and is checked during Hibernate validation
  • 3Use precision and scale for DECIMAL columns — never map currency to a FLOAT
  • 4insertable=false / updatable=false exclude the column from INSERT / UPDATE SQL
  • 5columnDefinition provides raw DDL — useful for DB-specific types (TEXT, JSON, JSONB)
  • 6spring.jpa.hibernate.ddl-auto=validate checks your mappings against the actual schema at startup

Interview Questions

Sign in to ask Aria
1

What is the difference between nullable=false in @Column and @NotNull?

MediumInfosys
2

When would you use columnDefinition in @Column?

MediumTCS
3

How does updatable=false prevent a field from being overwritten?

EasyWipro
4

What naming strategy does Spring Boot apply to map camelCase fields to column names?

EasyAccenture
5

Why should you use DECIMAL(10,2) instead of DOUBLE for monetary values?

EasyAmazon

Ask Aria about @Column Mapping

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…