@Column Annotation JPA is used in JPA entity’s fields to customise such as name, length, nullable etc. mapping to database table columns. By default, JPA automatically maps entity class field names to table column names. This guide explains the purpose, syntax, and best practices for using @Column in Spring Boot JPA applications.
What is @Column Annotation in JPA?
@Column is a JPA annotation used to specify column properties such as:
- Custom column names
- Unique constraints
- Nullability
- Column length
- Precision for numeric fields
- It helps fine-tune how an entity field is stored in the database
Basic Usage of @Column in JPA
By default, JPA maps entity field names to database columns, but @Column allows customization.
Example: Default Column Mapping (Without @Column)
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class Product {
@Id
private Long id;
private String name; // Will be mapped as "name"
private double price; // Will be mapped as "price"
// Getters and Setters
}
Generated SQL Table:
CREATE TABLE product (
id BIGINT PRIMARY KEY,
name VARCHAR(255),
price DOUBLE
);
By Default:
- The field name maps to the column name.
- Field price maps to column price.
Example: Custom Column Mapping with @Column
import import jakarta.persistence.*;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "product_name", length = 100, nullable = false, unique = true)
private String name;
@Column(name = "product_price", precision = 10, scale = 2)
private double price;
// Getters and Setters
}
Generated SQL Table:
CREATE TABLE product (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
product_name VARCHAR(100) NOT NULL UNIQUE,
product_price DECIMAL(10,2)
);
Explanation of @Column Attributes
- name – It specifies the column name in the database.
- nullable – It specifies whether the column can be NULL (default: true).
- unique – It specifies if the column should have a unique constraint.
- length – It specifies the length of a String column (default: 255).
- precision – It specifies the precision for BigDecimal or double values.
- scale – It specifies the number of decimal places for BigDecimal values.
- insertable – It determines if the column should be included in SQL INSERT statements (default: true).
- updatable – It determines if the column should be included in SQL UPDATE statements (default: true).
- columnDefinition – It allows defining the exact SQL column definition.
- table – It specifies the table name if the entity maps to multiple tables.
Example of JPA Entity with @Column Attributes
import jakarta.persistence.*;
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "full_name", nullable = false, length = 100)
private String name;
@Column(name = "email", unique = true, nullable = false)
private String email;
@Column(name = "salary", precision = 10, scale = 2)
private BigDecimal salary;
@Column(name = "created_at", insertable = false, updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private Timestamp createdAt;
// Getters and Setters
}
Explanation
- name = “full_name” → Maps the field name to the column full_name.
- nullable = false → Ensures name cannot be NULL.
- length = 100 → Limits name column to 100 characters.
- unique = true → Ensures email is unique.
- precision = 10, scale = 2 → Allows up to 10 digits with 2 decimal places for salary.
- insertable = false, updatable = false → Prevents modification of created_at column.
- columnDefinition = “TIMESTAMP DEFAULT CURRENT_TIMESTAMP” → Defines a custom SQL column default value.
Difference Between @Column and @Table
Feature | @Column | @Table |
Purpose | Customizes individual column properties. | Customizes table properties such as schema, and indexes. |
Applies To | Fields within an entity class. | The entity class itself. |
Example Usage | @Column(name = “product_name”, length = 100) | @Table(name = “products”, schema = “store”) |
Example Using @Table with @Column
import jakarta.persistence.*;
@Entity
@Table(name = "products", schema = "store")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "product_name", length = 100, nullable = false)
private String name;
}
Best Practices for Using @Column in JPA
- Use @Column Only When Necessary: If default column mapping works (name -> name), there’s no need for @Column.
- Keep Column Names Consistent: Use @Column(name = “snake_case_name”) to maintain consistency in database schemas.
- Set Length for String Columns: If the column should store long text, specify length = 500 or higher.
- Define Precision and Scale for Decimals: Use precision and scale for DECIMAL fields to prevent data loss.
- Avoid Unnecessary nullable = true Constraints: The default behaviour is NULL allowed, so explicitly defining it is unnecessary.
- Use Unique Constraints Wisely: Avoid using unique = true for non-essential fields to improve performance.
Conclusion
The @Column annotation of JPA provides fine-grained control over how the JPA entity’s properties are mapped to database table columns using the Java Bean class. It is used to customize column names, length, etc… It enhances readability and optimizes database structure for performance.
References:
From Oracle Documentation.