EntityManager em = null;
try {
em = emf.createEntityManager();
Book book = new Book();
book.setTitle("The Grapes of Wrath");
book.setAuthor("John Steinbeck");
book.setCopyrightYear(1939);
Date authorBirthdate =
new GregorianCalendar(1902, Calendar.FEBRUARY, 27).getTime();
book.setAuthorBirthdate(authorBirthdate);
ShortBlob coverIcon = new ShortBlob(new byte[] { 1, 2, 126, 127 });
book.setCoverIcon(coverIcon);
// stored as a multi-valued property
book.setTags(Arrays.asList("depression", "grapes", "wrath"));
// not stored (@Transient)
book.setDebugAccessCount(9999);
// stored as long_description
book.setLongDescription("...");
// stored but not indexed
book.setFirstSentence("...");
// Serializable field type stored as a datastore blob property,
// not indexed
PublisherMetadata publisherMetadata = new PublisherMetadata();
publisherMetadata.setItemCode("X1841GH9");
Date productionStartDate =
new GregorianCalendar(2002, Calendar.APRIL, 28).getTime();
publisherMetadata.setProductionStartDate(productionStartDate);
Date productionEndDate =
new GregorianCalendar(2002, Calendar.JULY, 7).getTime();
publisherMetadata.setProductionEndDate(productionEndDate);
book.setPublisherMetadata(publisherMetadata);
// JPA embedded object, stored as multiple properties on the
// Book entity.
Publisher publisher = new Publisher();
publisher.setName("GM Classics");
publisher.setMailingAddress("123 Paper St., Schenectady, NY, 12345");
book.setPublisher(publisher);
em.persist(book);
} finally {
em.close();