Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void removeTownsEx13() throws IOException {
- System.out.println("Addresses in Sofia will be more if you don't clean DB before");
- System.out.println("Type a town name:");
- String townName = reader.readLine();
- entityManager.getTransaction().begin();
- Town townDelete = this.entityManager.createQuery("SELECT t FROM Town t "
- + "WHERE t.name = :town", Town.class)
- .setParameter("town", townName)
- .getSingleResult();
- List<Address> addressesToDelete = this.entityManager
- .createQuery("SELECT a FROM Address a WHERE a.town.id= :id", Address.class)
- .setParameter("id", townDelete.getId())
- .getResultList();
- addressesToDelete
- .forEach(t -> t.getEmployees()
- .forEach(em -> em.setAddress(null)));
- addressesToDelete.forEach(this.entityManager::remove);
- this.entityManager.remove(townDelete);
- entityManager.getTransaction().commit();
- int countDeletedAddresses = addressesToDelete.size();
- System.out.printf("%d address%s in %s deleted",
- countDeletedAddresses,
- countDeletedAddresses == 1 ? "" : "es",
- townName);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement