Card 05/ 07
All 7 cards
ComparisonDifficulty: Advanced1 min
refresh() vs clear()
Both sound like they undo something. They undo completely different things, on completely different scopes.
before refresh, in-memory status = open
after refresh(), in-memory status = modified-directly
before clear(), contains? true
after clear(), contains? false| Compared on | refresh() | clear() |
|---|---|---|
| Scope | One entity | Every managed entity |
| Touches the database? | Yes — issues a fresh SELECT | No |
| What happens to unsaved local changes | Discarded, overwritten by the database's real values | Kept in memory, but no longer tracked or ever going to be saved |
refresh() is for one specific worry: "has this row changed underneath me?" — it reloads and overwrites whatever local changes existed. clear() is for a different one entirely — detaching everything at once, usually to stop a long-running batch operation from accumulating an ever-growing persistence context in memory. Reach for refresh() to get current data; reach for clear() to stop tracking, not to get anything.