Challenge - 5 Problems
JPA Entity Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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 }
Attempts:
2 left
💡 Hint
Every JPA entity must have a field annotated with @Id to identify the primary key.
✗ Incorrect
JPA requires an @Id field to uniquely identify each entity instance. Without it, saving the entity causes a runtime exception because JPA cannot manage the entity's identity.
📝 Syntax
intermediate2: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.
Attempts:
2 left
💡 Hint
The @Id annotation must be on a field or getter method representing the primary key.
✗ Incorrect
Option B correctly places @Id on a private field. Option B misses @Id. Option B places @Id without a field. Option B places @Id on a method that is not a getter.
❓ state_output
advanced2: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)?
Attempts:
2 left
💡 Hint
Persisting an entity means it becomes managed by the persistence context.
✗ Incorrect
Calling persist() makes the entity managed, so changes to it will be tracked and saved to the database on flush or commit.
🔧 Debug
advanced2: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; } }
Attempts:
2 left
💡 Hint
JPA requires a public or protected no-argument constructor for entities.
✗ Incorrect
JPA needs a no-argument constructor to instantiate entities via reflection. Without it, a runtime error occurs.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Entity names must be unique within the persistence unit to avoid conflicts.
✗ Incorrect
Hibernate requires unique entity names. Duplicate names cause deployment errors to prevent ambiguity in queries.