Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Column mapping with @Column in Spring Boot
📖 Scenario: You are building a simple Spring Boot application to manage books in a library. Each book has a title and a number of pages. You want to map these fields to specific columns in a database table using the @Column annotation.
🎯 Goal: Create a Spring Boot entity class called Book with fields title and pages. Use the @Column annotation to map title to a database column named book_title and pages to a column named number_of_pages.
📋 What You'll Learn
Create a class named Book annotated with @Entity
Add a private field title of type String
Add a private field pages of type int
Use @Column(name = "book_title") on the title field
Use @Column(name = "number_of_pages") on the pages field
💡 Why This Matters
🌍 Real World
Mapping Java class fields to database columns is essential when working with databases in Spring Boot applications. It ensures your data is stored and retrieved correctly.
💼 Career
Understanding @Column mapping is a basic skill for Java developers working with Spring Boot and JPA for database operations.
Progress0 / 4 steps
1
Create the Book entity class with fields
Create a public class called Book and add two private fields: title of type String and pages of type int.
Spring Boot
Hint
Think of the Book class as a blueprint for book objects. Add the fields inside the class.
2
Add @Entity annotation to the Book class
Add the @Entity annotation above the Book class declaration to mark it as a JPA entity.
Spring Boot
Hint
The @Entity annotation tells Spring Boot this class maps to a database table.
3
Map the title field to book_title column
Add the @Column(name = "book_title") annotation above the title field to map it to the database column named book_title.
Spring Boot
Hint
The @Column annotation lets you specify the exact database column name for a field.
4
Map the pages field to number_of_pages column
Add the @Column(name = "number_of_pages") annotation above the pages field to map it to the database column named number_of_pages.
Spring Boot
Hint
Just like the title, use @Column to map the pages field to the correct column.
Practice
(1/5)
1. What is the main purpose of the @Column annotation in Spring Boot?
easy
A. To define a primary key for the entity
B. To create a new database table automatically
C. To configure the database connection settings
D. To map a Java field to a specific database column
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 D
Quick Check:
@Column maps field to column [OK]
Hint: Remember: @Column links fields to columns directly [OK]
Common Mistakes:
Confusing @Column with @Id for primary keys
Thinking @Column creates tables
Mixing @Column with database connection setup
2. Which of the following is the correct way to specify a column name and length using @Column?
easy
A. @Column(name = "user_name", length = 50)
B. @Column(columnName = "user_name", size = 50)
C. @Column(name = user_name, length = 50)
D. @Column(name = "user_name", maxLength = 50)
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 A
Quick Check:
Use name and length attributes correctly [OK]
Hint: Use 'name' and 'length' exactly in @Column [OK]
Common Mistakes:
Using wrong attribute names like columnName or maxLength
What is wrong if the application allows saving an entity with age = null without error?
medium
A. The database column is not set to NOT NULL, so nullable=false is ignored
B. The @Column annotation does not enforce nullability at the database level
C. The field type Integer allows null, so nullable=false has no effect
D. The entity is missing @NotNull validation annotation
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 A
Quick Check:
nullable=false affects DB schema, not Java validation [OK]
Hint: @Column nullable=false sets DB constraint, not Java checks [OK]
Common Mistakes:
Thinking nullable=false validates Java field nulls
Assuming Integer type disables nulls
Confusing @Column with validation annotations
5. You want to map a Java field 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?