Mapping Fields and Column Types

Harry · 11 Sep 2026 · 10 views

Mapping Fields and Column Types

Every entity field maps to a schema, and getting the details right matters: the wrong column name, nullable rule, or length creates schema drift and subtle bugs. JPA gives you explicit annotations to pin down the mapping instead of relying on defaults.

Column control

@Column names the column, marks nullability and uniqueness, and sets length or precision. This is where constraints become visible to Hibernate when it generates the schema.

@Entity
public class Article {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 200)
    private String title;

    @Column(columnDefinition = "TEXT")
    private String body;

    @Column(nullable = false, unique = true)
    private String slug;
}

Temporal and enum types

Use java.time types with @Temporal for legacy Date fields, or better, modern LocalDate and Instant, which Hibernate maps cleanly. For enums, prefer @Enumerated(EnumType.STRING) so the stored value stays readable and does not break when you reorder enum constants.

Precision for money

For monetary values use BigDecimal with @Column(precision = 10, scale = 2). Floating-point columns accumulate rounding errors, and a double that displays as 19.99 may not be exactly that. Money is exact, so persist it exactly.

Default column rules

By default columns are nullable and length and precision follow field types. Hibernate's naming strategy converts camelCase to snake_case when configured. Whatever convention you choose, keep it consistent across entities so schema reviews are predictable.

Key Points

  • @Column controls names, nullability, length, and precision.
  • Prefer java.time types over legacy Date fields.
  • Enums stored as STRING survive reordering safely.
  • Use BigDecimal with defined precision for money.
  • State constraints so generated schema matches intent.
Share this post:

Comments (0)

Please login or register to comment.