EntityManagerFactory emf = EMF.get();
EntityManager em = null;
try {
em = emf.createEntityManager();
Book book = new Book("978-0596522728");
book.setTitle("Programming Google App Engine");
book.setAuthor("Dan Sanderson");
book.setCopyrightYear(2010);
Date authorBirthdate =
new GregorianCalendar(1978, Calendar.JANUARY, 11).getTime();
book.setAuthorBirthdate(authorBirthdate);
em.persist(book);
} finally {
em.close();
}
try {
em = emf.createEntityManager();
EntityTransaction txn = em.getTransaction();
txn.begin();
try {
Book book = em.find(Book.class, "978-0596522728");
BookReview bookReview = new BookReview();
bookReview.setRating(5);
book.getBookReviews().add(bookReview);
// No need to explicitly persist() the BookReview
// because it is a field of Book.
// Persist all updates and commit.
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
}
}
} finally {
em.close();
}
try {
em = emf.createEntityManager();
Book book = em.find(Book.class, "978-0596522728");
if (book != null) {
out.println("<p>Ratings for <i>" + book.getTitle() + "</i>: ");
for (BookReview review : book.getBookReviews()) {
out.println("[" + review.getRating() + "]");
}
out.println("</ul>");
} else {
out.println("Could not find that book I was looking for...");