public class JPAMapTest extends JPATestCase {
public void testInsert() {
HasOneToManyMapJPA pojo = new HasOneToManyMapJPA();
pojo.setVal("First");
Book book1 = new Book();
book1.setAuthor("Billy Nomates");
book1.setTitle("Lonely Time");
book1.setFirstPublished(1981);
pojo.getBooksByName().put(book1.getTitle(), book1);
pojo.getBasicMap().put(1, "First Entry");
pojo.getBasicMap().put(2, "Second Entry");
// Persist it
String bookId = null;
String pojoId = null;
beginTxn();
em.persist(pojo);
commitTxn();
pojoId = pojo.getId();
bookId = book1.getId();
em.clear();
// Retrieve it and validate
beginTxn();
HasOneToManyMapJPA pojo2 = em.find(HasOneToManyMapJPA.class, pojoId);
assertNotNull(pojo2);
Map<String, Book> booksByName = pojo2.getBooksByName();
assertNotNull(booksByName);
assertEquals("Number of elements in first map is wrong", 1, booksByName.size());
assertTrue(booksByName.containsKey("Lonely Time"));
Book bk = booksByName.get("Lonely Time");
assertEquals("Book id is incorrect", bookId, bk.getId());
assertEquals("Book published is wrong", 1981, bk.getFirstPublished());
Map<Integer, String> basicMap = pojo2.getBasicMap();
assertNotNull(basicMap);
assertEquals("Number of elements in second map is wrong", 2, basicMap.size());
assertTrue(basicMap.containsKey(1));