Entity Lifecycle States — Concept Check
Questions on Entity Lifecycle States. An entity moves through four states: transient (new, no ID), managed (tracked by context), detached (disconnected after context closes), and removed (scheduled for DELETE).
Questions in this quiz
8 questions on Entity Lifecycle States — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer creates a new Order object and calls em.persist(order). What is the state of the order after this operation?
- AManaged
- BDetached
- CTransient
- DRemoved
2.An Order entity was managed but the persistence context closed. What is its current state?
- AManaged
- BTransient
- CRemoved
- DDetached
3.After calling em.remove(entity) on a managed entity, what happens on the next flush?
- AThe entity becomes detached
- BThe entity is inserted into the database
- CThe entity is deleted from the database
- DThe entity is reattached
4.A developer mistakenly calls em.persist() on a detached entity. What is the result?
- AThe entity is inserted again
- BThe entity is deleted
- CEntityExistsException is thrown
- DThe entity is reattached
5.An Order entity is detached and a developer calls em.merge(order). What is the state of the returned instance?
- ADetached
- BManaged
- CRemoved
- DTransient
6.A developer changes a field of a managed entity but does not call save(). What happens on flush?
- ANo changes are saved
- BAn INSERT statement is generated
- CThe entity becomes detached
- DAn UPDATE statement is generated
7.What happens if you call em.merge() on a transient entity?
- AAn exception is thrown
- BThe entity becomes detached
- CThe transient entity is directly managed
- DA new managed entity is created and persisted
8.A developer calls em.clear() in a transaction. What happens to the managed entities?
- AThey are removed from the database
- BThey are persisted again
- CThey remain managed
- DThey become detached