public void testPersistAThenRelation() throws Exception {
EntityManager em = emf.createEntityManager();
try {
// Persist Side A
UnownedJPAManyToManySideA a = new UnownedJPAManyToManySideA();
em.persist(a);
// Create and persist Side B with Side A, plus another Side B
UnownedJPAManyToManySideB b = new UnownedJPAManyToManySideB();
b.getAs().add(a);
a.getBs().add(b);
em.persist(b);
UnownedJPAManyToManySideB b2 = new UnownedJPAManyToManySideB();
b2.getAs().add(a);
a.getBs().add(b2);
em.persist(b2);
} catch (Exception e) {
NucleusLogger.GENERAL.error("Exception in persist", e);
fail("Exception in test : " + e.getMessage());
return;
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
if (emf.getCache() != null) {
emf.getCache().evictAll();
}
em = emf.createEntityManager();
try {
Query qa = em.createQuery("SELECT a FROM UnownedJPAManyToManySideA a");
List<UnownedJPAManyToManySideA> results1 = qa.getResultList();
assertNotNull(results1);
assertEquals("Incorrect number of side A", 1, results1.size());
UnownedJPAManyToManySideA a = results1.iterator().next();
Set<UnownedJPAManyToManySideB> bs = a.getBs();
assertNotNull(bs);
assertEquals("Incorrect number of side B for A", 2, bs.size());
Query qb = em.createQuery("SELECT b FROM UnownedJPAManyToManySideB b");
List<UnownedJPAManyToManySideB> results2 = qb.getResultList();