๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
2026-03-01 10:30:11.500 ERROR 8842 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]] with root cause
java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`mydb`.`orders`, CONSTRAINT `fk_user_orders` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`))โก Quick Fix Works 80% of the time
Delete the child records (orders) before deleting the parent record (user), or use CascadeType.REMOVE.
@Entity
public class User {
@OneToMany(cascade = CascadeType.REMOVE)
private List<Order> orders;
}๐ง Why this Happens
Tap to expand the deep technical explanation
Your database has a foreign key constraint linking a child table (e.g., orders) to a parent table (e.g., users). You attempted to delete the parent row, but the database blocked the action because child rows still exist that reference it. This prevents orphaned records.
The HITEC City Parking Spot Analogy:
Imagine trying to demolish an apartment building, but there are still tenants living inside. The city (Database) refuses to grant the demolition permit until you evict the tenants (delete child records) first.
๐ How to Reproduce Confirm this is your error
Delete a parent row (a user) while child rows (orders) still reference it through a foreign key. The database throws 'Cannot delete or update a parent row: a foreign key constraint fails'.
๐ ๏ธ Solutions (3 Ways to Fix)
Use CascadeType.REMOVE
๐ Use this if you want deleting the parent to automatically delete the children.
This tells Hibernate: 'When I delete this User, automatically delete all their Orders first.' This prevents the foreign key violation.
@Entity
public class User {
@Id
private Long id;
@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE)
private List<Order> orders;
}Manually delete children first
๐ Use this if you don't want cascading deletes, or if you need to perform cleanup logic on children.
Query the database for the child records, delete them, and then delete the parent.
@Service
public class UserService {
@Transactional
public void deleteUser(Long userId) {
// 1. Delete children first
orderRepository.deleteByUserId(userId);
// 2. Delete parent
userRepository.deleteById(userId);
}
}Nullify the foreign key
๐ Use this if you want to keep the child records but sever the relationship.
Set the child's parent reference to null before deleting the parent. This requires the foreign key column to be nullable.
@Transactional
public void deleteUser(Long userId) {
List<Order> orders = orderRepository.findByUserId(userId);
for (Order o : orders) {
o.setUser(null); // Sever the link
}
orderRepository.saveAll(orders);
userRepository.deleteById(userId);
}๐ Version Notes
Uses Hibernate 5. Cascade operations are straightforward.
Uses Hibernate 6. Stricter orphan removal and cascade checks.
๐ก๏ธ How to Prevent This Next Time
Design your entity relationships carefully. Use CascadeType.REMOVE for parent-owned relationships, or implement soft deletes (is_deleted = true) to avoid hard deletes.