Key mainKey = KeyFactory.createKey(HasVersionMain.class.getSimpleName(), 1);
// Persist a Main
em.getTransaction().begin();
HasVersionMain m = new HasVersionMain(mainKey);
em.persist(m);
em.getTransaction().commit();
em.close();
// Add a Sub to the Main
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
HasVersionMain main = em.find(HasVersionMain.class, mainKey);
HasVersionSub s = new HasVersionSub();
s.setKey(mainKey, "Init db #0");
main.getSubs().add(s);
em.getTransaction().commit();
em.close();
// Get and detach Main (1)
em = emf.createEntityManager();
em.getTransaction().begin();
HasVersionMain m1 = em.find(HasVersionMain.class, mainKey);
m1.getSubs(); // Make sure subs are loaded
HasVersionSub s1 = m1.getSubs().get(0);
s1.getValue(); s1.getVersion();
assertNotNull("Version on element is null", JDOHelper.getVersion(s1));
em.getTransaction().commit();
em.close();
// Get and detach Main (2)
em = emf.createEntityManager();
em.getTransaction().begin();
HasVersionMain m2 = em.find(HasVersionMain.class, mainKey);
m2.getSubs(); // Make sure subs are loaded
HasVersionSub s2 = m2.getSubs().get(0);
s2.getValue(); s2.getVersion();
assertNotNull("Version on element is null", JDOHelper.getVersion(s2));
em.getTransaction().commit();
em.close();