The @Column annotation helps connect a class field to a specific database column. It tells Spring Boot how to save and read data from the database.
Column mapping with @Column in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
@Column(name = "column_name", nullable = false, length = 100, unique = true)
name sets the exact database column name.
nullable controls if the column can be empty (true or false).
email field to the user_email column in the database.@Column(name = "user_email")
private String email;username cannot be empty and limits it to 50 characters.@Column(nullable = false, length = 50)
private String username;phoneNumber is unique in the database.@Column(unique = true) private String phoneNumber;
This example shows a User entity where the name field is mapped to the user_name column with rules: it cannot be empty and has max length 100. The email field must be unique in the database.
import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Column; @Entity public class User { @Id private Long id; @Column(name = "user_name", nullable = false, length = 100) private String name; @Column(unique = true) private String email; // Constructors, getters, setters public User() {} public User(Long id, String name, String email) { this.id = id; this.name = name; this.email = email; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
If you don't use @Column, Spring Boot uses the field name as the column name by default.
Use @Column to avoid mistakes when your database column names differ from your Java fields.
Always check your database schema matches your entity mappings to avoid errors.
@Column links a Java field to a database column.
You can set column name, length, uniqueness, and nullability.
This helps keep your Java code and database in sync.
Practice
@Column annotation in Spring Boot?Solution
Step 1: Understand the role of @Column
The @Column annotation is used to link a Java class field to a database column, specifying details like name and constraints.Step 2: Differentiate from other annotations
Other annotations like @Id define primary keys, and database connection settings are configured elsewhere, not with @Column.Final Answer:
To map a Java field to a specific database column -> Option DQuick Check:
@Column maps field to column [OK]
- Confusing @Column with @Id for primary keys
- Thinking @Column creates tables
- Mixing @Column with database connection setup
@Column?Solution
Step 1: Check correct attribute names
The correct attributes for @Column are 'name' for column name and 'length' for string length.Step 2: Validate syntax
Attributes must be strings in quotes for names; length is an integer. @Column(name = "user_name", length = 50) uses correct syntax and attribute names.Final Answer:
@Column(name = "user_name", length = 50) -> Option AQuick Check:
Use name and length attributes correctly [OK]
- Using wrong attribute names like columnName or maxLength
- Forgetting quotes around string values
- Using variables without quotes for name
@Column(name = "email", unique = true, nullable = false) private String email;
What will happen if you try to save two entities with the same email?
Solution
Step 1: Understand unique constraint
The 'unique = true' attribute enforces that no two rows can have the same value in this column.Step 2: Predict behavior on duplicate insert
Trying to save a duplicate email will cause the database to reject the insert/update with a unique constraint violation error.Final Answer:
The database will throw a unique constraint violation error -> Option BQuick Check:
unique = true prevents duplicates [OK]
- Assuming duplicates are allowed with unique=true
- Confusing unique with nullable
- Expecting application to handle duplicates silently
@Column(name = "age", nullable = false) private Integer age;
What is wrong if the application allows saving an entity with
age = null without error?Solution
Step 1: Understand nullable attribute
The 'nullable = false' in @Column tells JPA to generate a NOT NULL constraint in the database schema.Step 2: Recognize runtime enforcement
@Column does not validate null values in Java code; it only affects database schema. If schema is not updated, nulls can be saved.Final Answer:
The database column is not set to NOT NULL, so nullable=false is ignored -> Option AQuick Check:
nullable=false affects DB schema, not Java validation [OK]
- Thinking nullable=false validates Java field nulls
- Assuming Integer type disables nulls
- Confusing @Column with validation annotations
private String phoneNumber; to a database column named phone_num that must be unique and cannot be null, with a maximum length of 15 characters. Which is the correct @Column annotation to use?Solution
Step 1: Match column name and constraints
The column name must be 'phone_num', unique true, nullable false, and length 15 as per requirements.Step 2: Check each option
@Column(name = "phone_num", unique = true, nullable = false, length = 15) matches all requirements exactly. @Column(name = "phoneNumber", unique = true, nullable = false, length = 15) uses wrong column name. @Column(name = "phone_num", unique = false, nullable = true, length = 15) allows null and not unique. @Column(name = "phone_num", unique = true, nullable = false) misses length attribute.Final Answer:
@Column(name = "phone_num", unique = true, nullable = false, length = 15) -> Option CQuick Check:
Use correct name, unique, nullable, and length [OK]
- Using Java field name instead of column name
- Forgetting unique or nullable constraints
- Missing length attribute for string columns
