public Person2 createPerson2(EntityManager em, int id) { Person2 p = new Person2(); p.setName("p_" + id); MedicalHistory2 m = createMedicalHistory2(em, mId2++); m.setPatient(p); // automatically set the id p.setMedical(m); em.persist(m); medicals2.put(m.getName(), m); em.persist(p); persons2.put(p.getName(), p); return p; }