Enterprise Java Development@TOPIC@

Chapter 40. Entity Manager Membership Methods

40.1. contains()
40.2. clear()
40.3. detach()
boolean contains(Object entity);
void clear();
em.persist(author);


//callers can detach entity from persistence context
logger.debug("em.contains(author)={}", em.contains(author));
logger.debug("detaching author");
em.getTransaction().begin();
em.flush();
em.detach(author);
logger.debug("em.contains(author)={}", em.contains(author));
em.getTransaction().commit();
//changes to detached entities do not change database
author.setFirstName("foo");
em.getTransaction().begin();
em.getTransaction().commit();
Author author2 = em.find(Author.class, author.getId());
logger.debug("author.firstName={}", author.getFirstName());
logger.debug("author2.firstName={}", author2.getFirstName());
assertNotEquals("unexpected name change", author.getFirstName(), author2.getFirstName());
-em.contains(author)=true
-detaching author
-em.contains(author)=false
-author.firstName=foo
-author2.firstName=dr