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 does the @Entity annotation do in a Spring Boot application?
The @Entity annotation marks a class as a JPA entity, meaning it represents a table in the database. It tells Spring Boot and JPA to manage this class for database operations.
Click to reveal answer
beginner
Which annotation is required to specify the primary key in a JPA entity?
The @Id annotation is used to mark a field as the primary key of the entity. This field uniquely identifies each record in the database table.
Click to reveal answer
intermediate
Why should a JPA entity class have a no-argument constructor?
JPA requires a no-argument constructor so it can create instances of the entity using reflection. This constructor can be public or protected.
Click to reveal answer
intermediate
What is the default table name for a JPA entity if @Table annotation is not used?
If @Table is not specified, the default table name is the same as the entity class name, usually case-sensitive depending on the database.
Click to reveal answer
beginner
How do you map a simple Java class to a database table using JPA?
You add the @Entity annotation to the class, mark a field with @Id for the primary key, and optionally use @Column to customize column mapping.
Click to reveal answer
What annotation marks a class as a JPA entity?
A@Entity
B@Table
C@Component
D@Repository
✗ Incorrect
The @Entity annotation tells JPA that the class is an entity representing a database table.
Which annotation identifies the primary key field in a JPA entity?
A@PrimaryKey
B@Id
C@Key
D@Column
✗ Incorrect
The @Id annotation marks the primary key field in a JPA entity.
What happens if a JPA entity class does not have a no-argument constructor?
AJPA will create the entity anyway
BCompilation error
CThe entity will be ignored
DRuntime error when JPA tries to instantiate the entity
✗ Incorrect
JPA uses reflection to create entity instances and requires a no-argument constructor; otherwise, it throws a runtime error.
If you don't specify @Table, what table name does JPA use?
AThe package name
BThe database default
CThe class name
DNo table is created
✗ Incorrect
By default, JPA uses the entity class name as the table name.
Which annotation customizes the column name in a JPA entity?
A@Column
B@Table
C@Entity
D@Id
✗ Incorrect
@Column lets you specify the column name and other properties for a field in the database.
Explain how to create a simple JPA entity class using @Entity and @Id annotations.
Think about how the class maps to a database table and how JPA identifies each record.
You got /4 concepts.
Describe why the no-argument constructor is important in a JPA entity.
Consider how JPA creates objects behind the scenes.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @Entity annotation in a Spring Boot application?
easy
A. To mark a class as a database table for JPA
B. To create a REST API endpoint
C. To configure application properties
D. To define a service component
Solution
Step 1: Understand the role of @Entity
The @Entity annotation tells Spring Boot and JPA that this class represents a table in the database.
Step 2: Differentiate from other annotations
Other annotations like @RestController or @Service serve different purposes unrelated to database tables.
Final Answer:
To mark a class as a database table for JPA -> Option A
Quick Check:
@Entity marks database tables [OK]
Hint: Remember: @Entity means database table class [OK]
Common Mistakes:
Confusing @Entity with @Service
Thinking @Entity creates REST endpoints
Assuming @Entity configures app settings
2. Which of the following is the correct way to declare a JPA entity class with an ID field?
easy
A. @Entity public class User { private Long id; }
B. public class User { private Long id; }
C. @Entity public class User { @Id private Long id; }
D. @Service public class User { @Id private Long id; }
Solution
Step 1: Check for @Entity annotation
The class must have @Entity to be recognized as a JPA entity.
Step 2: Verify presence of @Id on a field
Every entity needs a unique identifier marked with @Id to map the primary key.
Final Answer:
@Entity public class User { @Id private Long id; } -> Option C
Quick Check:
Entity + Id field = correct syntax [OK]
Hint: Entity needs @Entity and @Id on ID field [OK]
Common Mistakes:
Missing @Entity annotation
Forgetting @Id on the ID field
Using @Service instead of @Entity
3. Given this entity class:
@Entity
public class Product {
@Id
private Long id;
private String name;
public Product() {}
public Product(Long id, String name) {
this.id = id;
this.name = name;
}
public String getName() { return name; }
}
What will happen if you save a Product with id=1 and name="Book" using JPA repository and then retrieve it?
medium
A. The Product is not saved because of missing @Column
B. You get a Product object with id=1 and name="Book"
C. You get a runtime error because no setter for name
D. You get a Product object with id=1 but name is null
Solution
Step 1: Understand JPA entity saving and retrieval
JPA uses the @Entity class and its fields to save and load data. The constructor and getter allow access to fields.
Step 2: Check if missing setter affects retrieval
JPA can set fields via reflection even without setters, so name will be loaded correctly.
Final Answer:
You get a Product object with id=1 and name="Book" -> Option B
Quick Check:
JPA loads fields even without setters [OK]
Hint: JPA sets fields directly; getters needed to read [OK]
Common Mistakes:
Assuming missing setter causes runtime error
Thinking @Column is mandatory for saving
Believing name will be null without setter
4. Identify the error in this entity class:
@Entity
public class Customer {
private Long id;
private String email;
public Long getId() { return id; }
public String getEmail() { return email; }
}
medium
A. Missing @Entity annotation
B. Missing default constructor
C. Fields should be public
D. Missing @Id annotation on the id field
Solution
Step 1: Check for @Entity annotation
The class has @Entity, so it is recognized as an entity.
Step 2: Verify presence of @Id annotation
The id field lacks @Id, so JPA cannot identify the primary key, causing errors.
Final Answer:
Missing @Id annotation on the id field -> Option D
Quick Check:
Every entity needs @Id on primary key [OK]
Hint: Always put @Id on the primary key field [OK]
Common Mistakes:
Assuming default constructor is mandatory (JPA provides one)
Thinking fields must be public
Ignoring missing @Id annotation
5. You want to create a JPA entity Order with a composite primary key made of orderId and productId. Which approach correctly applies the @Entity annotation and primary key setup?
hard
A. Use @Entity on Order and create a separate @Embeddable class for the composite key with @EmbeddedId in Order
B. Use @Entity on Order and mark both fields @Id without extra class
C. Use @Entity on Order and mark only one field @Id, ignore the other
D. Use @Entity on Order and define composite key in application.properties
Solution
Step 1: Understand composite keys in JPA
JPA requires a separate class annotated with @Embeddable to represent composite keys.
Step 2: Use @EmbeddedId in the entity
The entity class uses @EmbeddedId to include the composite key class as its primary key.
Final Answer:
Use @Entity on Order and create a separate @Embeddable class for the composite key with @EmbeddedId in Order -> Option A