Challenge - 5 Problems
Cascade Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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 instanceAttempts:
2 left
💡 Hint
Think about what CascadeType.REMOVE means for related entities.
✗ Incorrect
CascadeType.REMOVE causes the persistence provider to delete all related child entities automatically when the parent is deleted.
❓ state_output
intermediate2: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);Attempts:
2 left
💡 Hint
CascadeType.PERSIST propagates the persist operation to children.
✗ Incorrect
CascadeType.PERSIST ensures that when the parent is persisted, all new child entities are also persisted automatically.
📝 Syntax
advanced2: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;
}Attempts:
2 left
💡 Hint
Look for the correct Java syntax for arrays in annotations.
✗ Incorrect
The cascade attribute expects an array of CascadeType enums enclosed in curly braces {}.
🔧 Debug
advanced2: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);
Attempts:
2 left
💡 Hint
CascadeType.ALL includes REMOVE, but deleting orphans requires a separate setting.
✗ Incorrect
CascadeType.ALL includes REMOVE, but to delete child entities removed from the parent's collection, orphanRemoval=true must be set.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about what happens if you delete one side of a shared relationship.
✗ Incorrect
CascadeType.REMOVE on ManyToMany can delete entities shared by multiple parents, causing data loss.