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
Recall & Review
beginner
What is the purpose of the @Column annotation in Spring Boot?
The @Column annotation maps a Java class field to a specific column in a database table. It helps customize column properties like name, length, and nullability.
Click to reveal answer
beginner
How do you specify a custom column name using @Column?
Use the 'name' attribute inside @Column, for example: @Column(name = "user_email") maps the field to the 'user_email' column in the table.
Click to reveal answer
beginner
What does setting nullable = false in @Column do?
It makes the database column NOT NULL, meaning the column must always have a value and cannot be left empty.
Click to reveal answer
beginner
Can @Column control the length of a String column? How?
Yes, by setting the 'length' attribute, e.g., @Column(length = 100) limits the string column to 100 characters.
Click to reveal answer
beginner
What happens if you omit the @Column annotation on a field?
Spring Boot uses default mapping: the field name becomes the column name, and default settings apply for length, nullability, etc.
Click to reveal answer
Which attribute of @Column sets the database column name?
Aname
Blength
Cnullable
Dunique
✗ Incorrect
The 'name' attribute specifies the exact column name in the database.
What does @Column(nullable = false) enforce?
AColumn length is unlimited
BColumn can be empty
CColumn is unique
DColumn must have a value
✗ Incorrect
Setting nullable = false means the column cannot be empty (NOT NULL).
How do you limit a String column to 50 characters using @Column?
A@Column(length = 50)
B@Column(size = 50)
C@Column(max = 50)
D@Column(limit = 50)
✗ Incorrect
The 'length' attribute controls the maximum size of a String column.
If you do not use @Column on a field, what happens?
AField is ignored
BError occurs
CDefault column mapping applies
DField becomes primary key
✗ Incorrect
Without @Column, default mapping uses the field name as the column name.
Which @Column attribute ensures column values are unique?
Aname
Bunique
Clength
Dnullable
✗ Incorrect
The 'unique' attribute enforces uniqueness of column values.
Explain how to use @Column to customize a database column in Spring Boot.
Think about how you control column name, size, and whether it can be empty.
You got /4 concepts.
Describe what happens if you omit the @Column annotation on a field in a Spring Boot entity.
Consider the default mapping rules Spring Boot applies.
You got /3 concepts.
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?