0
0
SpringbootConceptBeginner · 3 min read

@Column Annotation in Spring Boot: What It Is and How to Use

In Spring Boot, the @Column annotation is used to map a Java class field to a specific column in a database table. It lets you customize column properties like name, length, and whether it can be null, helping control how data is stored and retrieved.
⚙️

How It Works

The @Column annotation works like a label that connects a Java field to a database column. Imagine you have a spreadsheet where each column has a name and rules about what kind of data it can hold. The annotation tells Spring Boot exactly which column in the database matches the field in your Java class.

This helps Spring Boot understand how to save and load data correctly. For example, you can specify if the column should allow empty values, how long text can be, or even the exact name of the column if it differs from the field name. This makes your Java code and database stay in sync smoothly.

💻

Example

This example shows a simple Java entity class where @Column customizes the database column for a user's email.

java
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Column;

@Entity
public class User {
    @Id
    private Long id;

    @Column(name = "user_email", nullable = false, length = 100)
    private String email;

    // Constructors, getters, setters
    public User() {}

    public User(Long id, String email) {
        this.id = id;
        this.email = email;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
Output
When saved, the 'email' field is stored in the 'user_email' column of the database table, which does not allow null values and limits text length to 100 characters.
🎯

When to Use

Use @Column when you want to control how a Java field maps to a database column beyond the default settings. For example, if the database column name is different from your Java field name, or if you want to set limits on text length or prevent null values.

It is especially useful in real-world apps where database schemas have specific rules or legacy column names. This annotation helps keep your Java code clean and your database consistent.

Key Points

  • @Column links a Java field to a database column.
  • You can customize column name, length, nullability, and more.
  • It ensures your Java entity matches the database schema.
  • Helps prevent errors by enforcing database rules in your code.

Key Takeaways

The @Column annotation maps a Java field to a specific database column.
Use it to customize column properties like name, length, and nullability.
It helps keep your Java entities aligned with your database schema.
Applying @Column prevents data mismatches and enforces database rules.
It is essential for working with legacy databases or custom schemas.