Java Enterprise tutorial
http://piotrnowicki.com/2012/11/types-of-entitymanagers-application-managed-entitymanager/
see "EntityManager, Transaction and Everything"
em.find(Customer.class, custID);
Entity states:
new - has no persistent id and not associated with persistence context
managed - has id, associated
detached - has id, not currently associated
removed - has id, are associated, are scheduled for removal from data store
Persisting:
by invoking EntityManager's persist method
if cascade=PERSIST or cascade=ALL, related entities will be persisted
if entity is NEW, will be persisted to database
if entity is MANAGED, ignored (still cascade to related entities)
if entity is REMOVED, entity become managed
if entity is DETACHED, exception will be thrown (IllegalArgumentException)
Removing:
by invoking EntityManager's remove method
if cascade=REMOVE or cascade=ALL, related entities will be removed
if entity is NEW - ignored (still cascade)
if entity is DETACHED - exception
if entity is REMOVED - ignored
if entity is MANAGED - will remove from data store when transaction complete or by flush() operatioin
Synchronizing Entity Data to Database
When transaction that the entity is associated with commits, entity persistence state is synchronized.
To force synchronization, call flush() method.
Cascade will work.