Bird
Raised Fist0
Spring Bootframework~20 mins

JPA entity with @Entity annotation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
JPA Entity Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when saving a JPA entity without an @Id field?
Consider a JPA entity class annotated with @Entity but missing the @Id annotation on any field. What happens when you try to save an instance of this entity using Spring Data JPA?
Spring Boot
import jakarta.persistence.Entity;

@Entity
public class Product {
    private String name;
    private double price;

    // getters and setters
}
AA runtime exception is thrown indicating no identifier is found.
BThe entity saves successfully with an auto-generated ID.
CThe entity saves but with a null ID value.
DThe application fails to start due to missing @Id.
Attempts:
2 left
💡 Hint
Every JPA entity must have a field annotated with @Id to identify the primary key.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a JPA entity with a primary key?
Select the code snippet that correctly defines a JPA entity class with an @Entity annotation and a primary key field.
A
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class User {
    @Id
    public void userId() {}
}
B
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class User {
    @Id
    private Long userId;
}
C
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class User {
    private Long userId;
    @Id
}
D
import jakarta.persistence.Entity;

@Entity
public class User {
    private Long userId;
}
Attempts:
2 left
💡 Hint
The @Id annotation must be on a field or getter method representing the primary key.
state_output
advanced
2:00remaining
What is the state of a JPA entity after calling EntityManager.persist()?
Given a new JPA entity instance, what is its state immediately after calling EntityManager.persist(entity)?
AManaged - the entity is now tracked and will be synchronized with the database.
BTransient - the entity is not yet associated with the persistence context.
CDetached - the entity is no longer managed by the persistence context.
DRemoved - the entity is marked for deletion from the database.
Attempts:
2 left
💡 Hint
Persisting an entity means it becomes managed by the persistence context.
🔧 Debug
advanced
2:00remaining
Why does this JPA entity cause a runtime error?
Examine the following JPA entity code. Why does it cause a runtime error when the application starts?
Spring Boot
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

@Entity
public class Order {
    @Id
    private Long id;

    private String description;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    // Missing no-argument constructor
    public Order(String description) {
        this.description = description;
    }
}
AThe entity class is missing the @Table annotation.
BThe @Id annotation is incorrectly placed on a private field.
CThe entity lacks a no-argument constructor required by JPA.
DThe getter and setter methods are missing for the description field.
Attempts:
2 left
💡 Hint
JPA requires a public or protected no-argument constructor for entities.
🧠 Conceptual
expert
3:00remaining
What happens if two JPA entities share the same @Entity name?
Two different classes are annotated with @Entity(name = "Customer"). What is the effect when both are used in the same Spring Boot application?
ABoth entities will be treated as one, merging their fields and causing data corruption.
BThe second entity will override the first silently without error.
CThe application will run normally, but queries must specify the full class name.
DHibernate will throw a deployment exception due to duplicate entity names.
Attempts:
2 left
💡 Hint
Entity names must be unique within the persistence unit to avoid conflicts.

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

  1. Step 1: Understand the role of @Entity

    The @Entity annotation tells Spring Boot and JPA that this class represents a table in the database.
  2. Step 2: Differentiate from other annotations

    Other annotations like @RestController or @Service serve different purposes unrelated to database tables.
  3. Final Answer:

    To mark a class as a database table for JPA -> Option A
  4. 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

  1. Step 1: Check for @Entity annotation

    The class must have @Entity to be recognized as a JPA entity.
  2. Step 2: Verify presence of @Id on a field

    Every entity needs a unique identifier marked with @Id to map the primary key.
  3. Final Answer:

    @Entity public class User { @Id private Long id; } -> Option C
  4. 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

  1. 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.
  2. Step 2: Check if missing setter affects retrieval

    JPA can set fields via reflection even without setters, so name will be loaded correctly.
  3. Final Answer:

    You get a Product object with id=1 and name="Book" -> Option B
  4. 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

  1. Step 1: Check for @Entity annotation

    The class has @Entity, so it is recognized as an entity.
  2. Step 2: Verify presence of @Id annotation

    The id field lacks @Id, so JPA cannot identify the primary key, causing errors.
  3. Final Answer:

    Missing @Id annotation on the id field -> Option D
  4. 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

  1. Step 1: Understand composite keys in JPA

    JPA requires a separate class annotated with @Embeddable to represent composite keys.
  2. Step 2: Use @EmbeddedId in the entity

    The entity class uses @EmbeddedId to include the composite key class as its primary key.
  3. Final Answer:

    Use @Entity on Order and create a separate @Embeddable class for the composite key with @EmbeddedId in Order -> Option A
  4. Quick Check:

    Composite key needs @Embeddable + @EmbeddedId [OK]
Hint: Composite keys need @Embeddable class + @EmbeddedId field [OK]
Common Mistakes:
  • Marking multiple fields with @Id without composite key class
  • Ignoring one key field in composite key
  • Trying to configure keys in properties file