Bird
Raised Fist0
Spring Bootframework~20 mins

Cascade types and behavior 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
🎖️
Cascade Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you delete a parent entity with CascadeType.REMOVE?
Consider a parent entity with a one-to-many relationship to child entities using CascadeType.REMOVE. What is the behavior when the parent entity is deleted?
Spring Boot
public class Parent {
  @OneToMany(cascade = CascadeType.REMOVE)
  private List<Child> children;
}

// Deleting a Parent instance
AChild entities are detached but not deleted; they remain unmanaged.
BOnly the parent entity is deleted; child entities remain in the database.
CDeleting the parent causes an error because child entities must be deleted first manually.
DBoth the parent and all its child entities are deleted from the database.
Attempts:
2 left
💡 Hint
Think about what CascadeType.REMOVE means for related entities.
state_output
intermediate
2:00remaining
What is the state of child entities after persisting a parent with CascadeType.PERSIST?
Given a parent entity with a one-to-many relationship to children using CascadeType.PERSIST, what happens to the child entities when the parent is saved for the first time?
Spring Boot
public class Parent {
  @OneToMany(cascade = CascadeType.PERSIST)
  private List<Child> children;
}

Parent parent = new Parent();
parent.getChildren().add(new Child());
entityManager.persist(parent);
AOnly the parent is saved; child entities remain transient and are not saved.
BBoth parent and child entities are saved to the database in the same transaction.
CChild entities are saved but parent remains transient.
DSaving the parent causes an exception because children are not explicitly saved.
Attempts:
2 left
💡 Hint
CascadeType.PERSIST propagates the persist operation to children.
📝 Syntax
advanced
2:00remaining
Which option correctly applies multiple cascade types to a relationship?
You want to apply both PERSIST and MERGE cascade types to a one-to-one relationship. Which code snippet is correct?
Spring Boot
public class User {
  @OneToOne(cascade = ???)
  private Profile profile;
}
Acascade = CascadeType.PERSIST, CascadeType.MERGE
Bcascade = [CascadeType.PERSIST, CascadeType.MERGE]
Ccascade = {CascadeType.PERSIST, CascadeType.MERGE}
Dcascade = (CascadeType.PERSIST, CascadeType.MERGE)
Attempts:
2 left
💡 Hint
Look for the correct Java syntax for arrays in annotations.
🔧 Debug
advanced
2:00remaining
Why does deleting a parent entity not delete child entities despite CascadeType.ALL?
Given this code, deleting the parent does not remove child entities. What is the likely cause? public class Parent { @OneToMany(cascade = CascadeType.ALL) private List children; } entityManager.remove(parent);
AThe orphanRemoval attribute is not set to true, so children are not deleted when removed from parent.
BCascadeType.ALL does not include REMOVE, so children are not deleted.
CThe parent entity is not managed, so remove does nothing.
DThe children collection is empty, so nothing happens.
Attempts:
2 left
💡 Hint
CascadeType.ALL includes REMOVE, but deleting orphans requires a separate setting.
🧠 Conceptual
expert
2:00remaining
Which cascade type should you avoid on a ManyToMany relationship to prevent unintended deletions?
In a ManyToMany relationship, which cascade type can cause dangerous side effects by deleting shared entities unintentionally?
ACascadeType.REMOVE
BCascadeType.MERGE
CCascadeType.PERSIST
DCascadeType.REFRESH
Attempts:
2 left
💡 Hint
Think about what happens if you delete one side of a shared relationship.

Practice

(1/5)
1. What does the CascadeType.ALL option do in Spring Boot JPA?
easy
A. It applies all cascade operations including persist, merge, remove, refresh, and detach.
B. It only cascades the persist operation to related entities.
C. It disables all cascade operations.
D. It only cascades the remove operation to related entities.

Solution

  1. Step 1: Understand CascadeType.ALL meaning

    CascadeType.ALL means all cascade operations are applied to related entities automatically.
  2. Step 2: Identify included operations

    These operations include persist, merge, remove, refresh, and detach, covering all common entity lifecycle events.
  3. Final Answer:

    It applies all cascade operations including persist, merge, remove, refresh, and detach. -> Option A
  4. Quick Check:

    CascadeType.ALL = all cascade operations [OK]
Hint: ALL means every cascade action is applied automatically [OK]
Common Mistakes:
  • Thinking ALL only means persist or remove
  • Confusing cascade with fetch types
  • Assuming cascade disables operations
2. Which of the following is the correct syntax to apply cascade persist on a JPA relationship in Spring Boot?
easy
A. @OneToMany(cascade = "persist")
B. @OneToMany(cascade = CascadeType.PERSIST)
C. @OneToMany(cascade = {"persist"})
D. @OneToMany(cascade = CascadeType.ALL.PERSIST)

Solution

  1. Step 1: Recall correct cascade syntax

    The cascade attribute expects an array or single enum value of type CascadeType.
  2. Step 2: Validate each option

    @OneToMany(cascade = CascadeType.PERSIST) uses correct enum CascadeType.PERSIST. String-based options like "persist" or {"persist"} are incorrect. CascadeType.ALL.PERSIST uses invalid enum chaining.
  3. Final Answer:

    @OneToMany(cascade = CascadeType.PERSIST) -> Option B
  4. Quick Check:

    Use CascadeType enums, not strings [OK]
Hint: Use CascadeType.PERSIST enum, not string quotes [OK]
Common Mistakes:
  • Using string values instead of enum constants
  • Trying to chain enums like ALL.PERSIST
  • Omitting the cascade attribute entirely
3. Given the following code snippet, what happens when parentRepository.delete(parent) is called?
@Entity
class Parent {
  @OneToMany(cascade = CascadeType.REMOVE)
  List<Child> children;
}
medium
A. An exception is thrown because cascade REMOVE is invalid here.
B. Only the parent entity is deleted; children remain in the database.
C. Both parent and all its children are deleted from the database.
D. Children are detached but not deleted; parent is deleted.

Solution

  1. Step 1: Understand CascadeType.REMOVE effect

    CascadeType.REMOVE means deleting the parent also deletes all related children automatically.
  2. Step 2: Apply to the delete operation

    Calling delete on parent triggers removal of parent and cascades delete to all children in the list.
  3. Final Answer:

    Both parent and all its children are deleted from the database. -> Option C
  4. Quick Check:

    REMOVE cascades delete to children [OK]
Hint: REMOVE cascades delete to related entities [OK]
Common Mistakes:
  • Assuming children remain after parent delete
  • Confusing REMOVE with DETACH or REFRESH
  • Thinking cascade REMOVE causes errors
4. Identify the error in this code snippet related to cascade types:
@Entity
class Order {
  @OneToOne(cascade = CascadeType.MERGE)
  Payment payment;
}

// Later in code
orderRepository.save(order);

What issue might occur?
medium
A. The payment entity will not be saved if it is new, causing an error.
B. The cascade MERGE will cause duplicate payment entries.
C. The code will throw a syntax error due to cascade misuse.
D. The order entity will not be saved because cascade is missing.

Solution

  1. Step 1: Understand CascadeType.MERGE behavior

    CascadeType.MERGE only updates existing entities; it does not persist new ones automatically.
  2. Step 2: Analyze save operation with new payment

    If payment is new (not yet saved), save(order) won't persist payment, causing an error or missing data.
  3. Final Answer:

    The payment entity will not be saved if it is new, causing an error. -> Option A
  4. Quick Check:

    MERGE does not persist new entities [OK]
Hint: MERGE updates only; use PERSIST to save new entities [OK]
Common Mistakes:
  • Assuming MERGE saves new entities
  • Confusing syntax errors with runtime behavior
  • Ignoring cascade effects on related entities
5. You have a Customer entity with a @OneToMany relationship to Order entities. You want to ensure that when a customer is deleted, all their orders are also deleted, but when an order is updated, the customer should not be affected. Which cascade type configuration is best?
hard
A. @OneToMany(cascade = CascadeType.DETACH)
B. @OneToMany(cascade = CascadeType.ALL)
C. @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
D. @OneToMany(cascade = CascadeType.REMOVE)

Solution

  1. Step 1: Identify cascade needed for delete

    To delete all orders when customer is deleted, CascadeType.REMOVE is required.
  2. Step 2: Avoid affecting customer on order update

    Using only REMOVE avoids cascading updates or persists from orders to customer, so customer stays unchanged on order update.
  3. Final Answer:

    @OneToMany(cascade = CascadeType.REMOVE) -> Option D
  4. Quick Check:

    REMOVE cascades delete only, no update cascade [OK]
Hint: Use REMOVE to cascade deletes only, not updates [OK]
Common Mistakes:
  • Using ALL cascades causing unwanted updates
  • Using PERSIST or MERGE alone missing delete cascade
  • Confusing DETACH with delete cascade