try {
em = emf.createEntityManager();
// An entity with a Key ID field. Can have an ancestor set
// in the Key.
Key book3key = new Builder("Publisher", "O'Reilly")
.addChild("BookKeyId", "978-0-596-52272-8")
.getKey();
BookKeyId book3 = new BookKeyId(book3key);
em.persist(book3);
} finally {
em.close();
}
try {
em = emf.createEntityManager();
// An object with its entity's key encoded as two String
// fields, one for the object's kind and (string or
// numeric) ID, and one for the parent. (The class
// constructor calls KeyFactory.keyToString() to encode
// the Key values. See BookEncodedStringId.java.)
Key book4keyParent = new Builder("Publisher", "O'Reilly").getKey();
Key book4key = new Builder("BookEncodedStringId", "978-0-596-52272-8")
.getKey();
BookEncodedStringId book4 = new BookEncodedStringId(book4key,
book4keyParent);
em.persist(book4);