Enterprise Java Development@TOPIC@
em.persist(author);
//callers can detach entity from persistence context
log.debug("em.contains(author)="+em.contains(author));
log.debug("detaching author");
em.getTransaction().begin();
em.flush();
em.detach(author);
log.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());
log.debug("author.firstName=" + author.getFirstName());
log.debug("author2.firstName=" + author2.getFirstName());
assertFalse("unexpected name change",
author.getFirstName().equals(author2.getFirstName()));
-em.contains(author)=true -detaching author -em.contains(author)=false -author.firstName=foo -author2.firstName=dr
Author author = new Author();
log.debug("em.contains(author)="+em.contains(author));
log.debug("detaching author");
em.detach(author);
log.debug("em.contains(author)="+em.contains(author));
-em.contains(author)=false -detaching author -em.contains(author)=false
Author author = new Author();
...
em.persist(author);
em.getTransaction().begin();
em.getTransaction().commit();
//detaching detached entity will be ignored
Author detached = new Author(author.getId());
log.debug("em.contains(author)="+em.contains(detached));
log.debug("detaching detached author");
em.detach(detached);
log.debug("em.contains(author)="+em.contains(detached));
-em.contains(author)=false -detaching detached author -em.contains(author)=false