public Cloud createAndSave() { Session session = sessions.openSession(); Transaction transaction = session.beginTransaction(); Cloud cloud = new Cloud(); cloud.setLength(length); for (String description : producedSnowflakes) { SnowFlake sf = new SnowFlake(); sf.setDescription(description); session.save(sf); cloud.getProducedSnowFlakes().add(sf); } for (String description : backupSnowflakes) { SnowFlake sf = new SnowFlake(); sf.setDescription(description); session.save(sf); cloud.getBackupSnowFlakes().add(sf); } session.persist(cloud); transaction.commit(); session.close(); return cloud; }
@After public void removeCloudAndSnowflakes() { Session session = sessions.openSession(); Transaction transaction = session.beginTransaction(); if (cloud != null) { Cloud cloudToDelete = (Cloud) session.get(Cloud.class, cloud.getId()); for (SnowFlake current : cloudToDelete.getProducedSnowFlakes()) { session.delete(current); } for (SnowFlake current : cloudToDelete.getBackupSnowFlakes()) { session.delete(current); } session.delete(cloudToDelete); } transaction.commit(); session.close(); assertThat(TestHelper.getNumberOfEntities(sessions)).isEqualTo(0); assertThat(TestHelper.getNumberOfAssociations(sessions)).isEqualTo(0); }