0
0
Spring Bootframework~20 mins

Cascade types and behavior in Spring Boot - Practice Problems & Coding Challenges

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