0
0
Spring Bootframework~15 mins

Column mapping with @Column in Spring Boot - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Just like the title, use @Column to map the pages field to the correct column.