0
0
Spring Bootframework~20 mins

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

Choose your learning style9 modes available
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.